This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.



Gets a substring beginning at the specified location and having the specified length.


stringvar.substr( start [, length ])
Required. A string literal or String object from which the substring is extracted.
Required. The starting position of the desired substring. The index of the first character in the string is zero.
Optional. The number of characters to include in the returned substring.


The following example illustrates the use of the substr method.

var s = "The quick brown fox jumps over the lazy dog.";
 var ss = s.substr(10, 5);
 document.write("[" + ss + "] <br>");

 ss = s.substr(10);
 document.write("[" + ss + "] <br>");

 ss = s.substr(10, -5);
 document.write("[" + ss + "] <br>");

ss = s.substr(-4);
document.write("[" + ss + "] <br>");
 // Output:
 // [brown]
 // [brown fox jumps over the lazy dog.]
 // []
 // [dog.]


If length is zero or negative, an empty string is returned. If not specified, the substring continues to the end of stringvar. If start is negative, it is treated as [stringvar.length+start] if length is omitted this would return the last `start` characters of the stringvar.

See also

Other articles


  • Microsoft Developer Network: Article