JavaScript substring()Method
JavaScript String Object
Definition and Usage
The substring() method is used to extract characters between two specified indices in a string.
The substring returned by the substring() method includesthe startthe character at, but does not includethe endthe character at.
Syntax
string.substring(from, to)
| Parameters | Description |
|---|---|
| from | Required. A non-negative integer specifying the position of the first character of the substring to extract in the string Object. |
| to | Optional. A non-negative integer that is one more than the position of the last character of the substring to extract in the string Object. If this parameter is omitted, the returned substring will continue to the end of the string. |
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the substring() method
Example
Example
In this example, we will use substring() to extract some characters from a string:
<script>
var str="Hello world!";
document.write(str.substring(3)+"<br>");
document.write(str.substring(3,7));
</script>
var str="Hello world!";
document.write(str.substring(3)+"<br>");
document.write(str.substring(3,7));
</script>
The output result of the above code:
lo world!
lo w
lo w
Try it Yourself »
JavaScript String Object
Other Extensions