Home >>JavaScript String Methods >JavaScript String substr() Method

JavaScript String substr() Method

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 Values

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.

Browser Support

Method Chrome Edge Firefox Safari Opera
substr() Yes Yes Yes Yes Yes
Here is an example of substr() method:
<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>
Output:

Click the button to see the Output.

Example 2:
<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>
Output:

Click the button to see the Output.