| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr | [Back] [Original] |
Get to know MDN better
Avoid using this feature in new projects. HTML wrapper methods exist only for backwards compatibility. Use DOM APIs to create elements instead. This feature may be a candidate for removal from web standards or browsers.
Consider using the following features instead: DOM.
The substr() method of String values returns a portion of this string, starting at the specified index and extending for a given number of characters afterwards.
Note:
substr() is not part of the main ECMAScript specification it's defined in Annex B: Additional ECMAScript Features for Web Browsers, which is normative optional for non-browser runtimes. Therefore, people are advised to use the standard String.prototype.substring() and String.prototype.slice() methods instead to make their code maximally cross-platform friendly. The String.prototype.substring() page has some comparisons between the three methods.
const str = "Mozilla";
console.log(str.substr(1, 2));
// Expected output: "oz"
console.log(str.substr(2));
// Expected output: "zilla"
substr(start)
substr(start, length)
startThe index of the first character to include in the returned substring.
length OptionalThe number of characters to extract.
A new string containing the specified part of the given string.
A string's substr() method extracts length characters from the string, counting from the start index.
start >= str.length, an empty string is returned.start < 0, the index starts counting from the end of the string. More formally, in this case the substring starts at max(start + str.length, 0).start is omitted or undefined, it's treated as 0.length is omitted or undefined, or if start + length >= str.length, substr() extracts characters to the end of the string.length < 0, an empty string is returned.start and length, NaN is treated as 0.Although you are encouraged to avoid using substr(), there is no trivial way to migrate substr() to either slice() or substring() in legacy code without essentially writing a polyfill for substr(). For example, str.substr(a, l), str.slice(a, a + l), and str.substring(a, a + l) all have different results when str = "01234", a = 1, l = -2 substr() returns an empty string, slice() returns "123", while substring() returns "0". The actual refactoring path depends on the knowledge of the range of a and l.
const string = "Mozilla";
console.log(string.substr(0, 1)); // 'M'
console.log(string.substr(1, 0)); // ''
console.log(string.substr(-1, 1)); // 'a'
console.log(string.substr(1, -1)); // ''
console.log(string.substr(-3)); // 'lla'
console.log(string.substr(1)); // 'ozilla'
console.log(string.substr(-20, 2)); // 'Mo'
console.log(string.substr(20, 2)); // ''
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.substr |
String.prototype.substr in core-jsString.prototype.substrString.prototype.slice()String.prototype.substring()This page was last modified on Aug 19, 2025 by MDN contributors.
Stringanchor()at()big()blink()bold()charAt()charCodeAt()codePointAt()concat()endsWith()fixed()fontcolor()fontsize()includes()indexOf()isWellFormed()italics()lastIndexOf()link()localeCompare()match()matchAll()normalize()padEnd()padStart()repeat()replace()replaceAll()search()slice()small()split()startsWith()strike()sub()substr()substring()sup()toLocaleLowerCase()toLocaleUpperCase()toLowerCase()toString()toUpperCase()toWellFormed()trim()trimEnd()trimStart()valueOf()[Symbol.iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |