Home >>JavaScript String Methods >JavaScript String substr() Method
JavaScript substr() method is used to extract some parts of a string beginning from the character at the specified position and return the specified number of characters from the given input string. It does not change the original given string.
Syntax:string.substr(start, length)
Parameter | Description |
---|---|
start | This is a required parameter. It defines the position where to start the extraction. |
length | This is an optional parameter. It defines the number of characters to extract. |
Method | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
substr() | Yes | Yes | Yes | Yes | Yes |
<html> <body> <p>Click the button to see the Output.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var str = "Welcome to our Website..."; var res = str.substr(0, 7); document.getElementById("demo").innerHTML = res; } </script> </body> </html>
Click the button to see the Output.
<html> <body> <p>Click the button to see the Output.</p> <button onclick="myFunction1()">Try it</button> <p id="demo1"></p> <script> function myFunction1() { var str = "Welcome to our Website."; var res = str.substr(15, 8); document.getElementById("demo1").innerHTML = res; } </script> </body> </html>
Click the button to see the Output.