JavaScript String method : substr()

The substr() method is used to copy or extract a part of the string start from the index position beginIndex to the provide lengthof the string. The syntax would be :
  str.trim(beginIndex, Length)
where the beginIndex is nothing but the start point where the trim() start copying characters till the length provided by Length parameter.

Example :
<html>
<body>
  <script>
    var str = "Hello world!!.";
    var newStr = str.substr(6, 5); 
    document.write(newStr);
  </script> 
</body>
</html>
Output :


We can also use negative number which copy the string from backwards.
<html>
<body>
  <script>
    var str = "Hello world!!.";
    var newStr = str.substr(-8, 5); 
    document.write(newStr);
  </script> 
</body>
</html>
Output :



Next Topic :