Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript substring() Method

JavaScript substring() Method

String Object Reference JavaScript String object


Definition and Usage

The substring() method is used to extract the characters in a string between two specified indices.

The substring returned by the substring() method includes at the beginning the character at the start position but does not include the character at the *end* position.

Syntax

string

.substring(from, to)

Parameters Description
from Required. A non-negative integer that specifies the position of the first character of the substring to extract in 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 string Object.
If this parameter is omitted, the returned substring extends to the end of the string.

Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the substring() method.


Example

Example

In this example, we will use substring() to extract some characters from the string::

The output of the above code:

lo world!

lo w

Try it »


String Object Reference JavaScript String object