String.substr() is not ECMAScript


February 26, 2013by @prust; javascript

I'm not sure why I never noticed before, but String.substr() isn't actually in the ECMAScript standard. The 3rd edition of the ECMAScript spec does mention it in the non-normative annex in order to "suggest uniforn semantics for such properties without making [them] part of this standard".

There are already two methods in the standard that do essentially the same thing, String.slice() and String.substring(). My preference is String.slice() because it supports negative indexes, doesn't have confusing argument-swapping behavior ("if indexA is larger than indexB, then the effect of substring is as if the two arguments were swapped") and its API is similar to .slice() for Arrays and string slicing in other languages.

comments powered by Disqus