String.prototype.substring()
The substring()
method returns the part of the string
from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
Syntax
substring(indexStart)
substring(indexStart, indexEnd)
Parameters
indexStart
- : The index of the first character to include in the returned substring.
indexEnd
optional- : The index of the first character to exclude from the returned substring.
Return value
A new string containing the specified part of the given string.
Description
substring()
extracts characters from indexStart
up to but not including indexEnd
. In particular:
- If
indexEnd
is omitted,substring()
extracts characters to the end of the string. - If
indexStart
is equal toindexEnd
,substring()
returns an empty string. - If
indexStart
is greater thanindexEnd
, then the effect ofsubstring()
is as if the two arguments were swapped; see example below.
Any argument value that is less than 0
or greater than str.length
is treated as if it were 0
and str.length
, respectively.
Any argument value that is NaN
is treated as if it were 0
.