gotchajavascriptCritical
What is the difference between substr and substring?
Viewed 0 times
substrandbetweenthedifferencesubstringwhat
Problem
What is the difference between
and
They both seem to output “ab”.
alert("abc".substr(0,2));and
alert("abc".substring(0,2));They both seem to output “ab”.
Solution
The difference is in the second argument. The second argument to
Links?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
substring is the index to stop at (but not include), but the second argument to substr is the maximum length to return.Links?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
Context
Stack Overflow Q#3745515, score: 1109
Revisions (0)
No revisions yet.