W3cubDocs

/JavaScript

string.substr

Warning: Although String.prototype.substr(…) is not strictly deprecated (as in "removed from the Web standards"), it is considered a legacy function and should be avoided when possible. It is not part of the core JavaScript language and may be removed in the future. If at all possible, use the substring() method instead.

The substr() method returns a portion of the string, starting at the specified index and extending for a given number of characters afterward.

Syntax

str.substr(start[, length])

Parameters

start
The index of the first character to include in the returned substring.
length
Optional. The number of characters to extract.

Return value

A new string containing the specified part of the given string.

Description

substr() extracts length characters from a string, counting from the start index.

If start is a positive number, the index starts counting at the start of the string. Its value is capped at str.length.
If start is a negative number, the index starts counting from the end of the string. Its value is capped at -str.length.
Note: In Microsoft JScript, negative values of the start argument are not considered to refer to the end of the string.

If length is omitted, substr() extracts characters to the end of the string.
If length is undefined, substr() extracts characters to the end of the string.
If length is a negative number, it is treated as 0.

For both start and length, NaN is treated as 0.

Examples

Using substr()

var aString = 'Mozilla';

console.log(aString.substr(0, 1));   // 'M'
console.log(aString.substr(1, 0));   // ''
console.log(aString.substr(-1, 1));  // 'a'
console.log(aString.substr(1, -1));  // ''
console.log(aString.substr(-3));     // 'lla'
console.log(aString.substr(1));      // 'ozilla'
console.log(aString.substr(-20, 2)); // 'Mo'
console.log(aString.substr(20, 2));  // ''

Polyfill

Microsoft's JScript does not support negative values for the start index. To use this feature in JScript, you can use the following code:

// only run when the substr() function is broken
if ('ab'.substr(-1) != 'b') {
  /**
   *  Get the substring of a string
   *  @param  {integer}  start   where to start the substring
   *  @param  {integer}  length  how many characters to return
   *  @return {string}
   */
  String.prototype.substr = function(substr) {
    return function(start, length) {
      // call the original method
      return substr.call(this,
      	// did we get a negative start, calculate how much it is from the beginning of the string
        // adjust the start parameter for negative value
        start < 0 ? this.length + start : start,
        length)
    }
  }(String.prototype.substr);
}

Specifications

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Defined in the (informative) Compatibility Annex B. Implemented in JavaScript 1.0.
ECMAScript 5.1 (ECMA-262)
The definition of 'String.prototype.substr' in that specification.
Standard Defined in the (informative) Compatibility Annex B
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'String.prototype.substr' in that specification.
Standard Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers
ECMAScript Latest Draft (ECMA-262)
The definition of 'String.prototype.substr' in that specification.
Draft Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes Yes 1 Yes Yes Yes
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes Yes Yes 4 Yes Yes Yes
Server
Node.js
Basic support Yes

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr