String.prototype.substr() | substring substr javascript
Astringssubstr()methodextractslengthcharactersfromthestring,countingfromthestartindex.Ifstart>=str.length,anemptystringisreturned.Ifstart<0,theindexstartscountingfromtheendofthestring.Moreformally,inthiscasethesubstringstartsatmax(start+str.length,0).Ifstartisomittedorundefined[1],itstreatedas0.Iflengthisomittedorundefined[2],orifstart+length>=str.length,substr()extractscharacterstotheendofthestring.Iflength<0,anemptystringisreturned.Forbothstartandlength,NaN[3]istreatedas0.Altho...
A strings substr() method extracts length characters from the string, counting from the start index.
If start >= str.length, an empty string is returned. If 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). If start is omitted or undefined[1], its treated as 0. If length is omitted or undefined[2], or if start + length >= str.length, substr() extracts characters to the end of the string. If length < 0, an empty string is returned. For both start and length, NaN[3] 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() retu...