JavaScript provides 2 similar looking String manipulation function, substr together with substring, though both are used to instruct substring from an String, at that spot is a subtle difference betwixt substring together with substr method inwards JavaScript. If y'all aspect at at that spot signature, both substr(to, length) together with substring(to, from) both takes 2 parameters, but substr takes length of substring to live returned, spell substring takes terminate index (excluding) for substring.This principal divergence volition live to a greater extent than clear when nosotros volition run into couplet of examples of using substr together with substring inwards JavaScript code. By the way this is likewise a skillful JavaScript interrogation together with oft asked to spider web developers during HTML, CSS together with JavaScript interviews, along alongside how to foreclose multiple submission of shape data. Because of extensive uses of String, this noesis is non exclusively of import from interview indicate of persuasion but likewise from evolution indicate of view. It likewise helps to cut back subtle bugs inwards JavaScript code, which is innovate because of wrong usage of substr or substring method.
As I said inwards offset paragraph, principal divergence betwixt substring together with substr JavaScript method are inwards at that spot mo parameter, substring convey index of lastly grapheme + 1, spell substr() method gets expected length of substring. Let's convey a aspect at next examples : Difference betwixt SubString together with SubStr method inwards JavaScript
var str = "JavaScript";
alert(str.substring(0, 4)); // would impress "Java"
Since JavaScript index are null based, In inwards a higher house instance starting index is null (including), which is offset grapheme together with terminate index is 4 (excluding), together with then substring would live "Java". On the other hand, if nosotros apply similar declaration to substr method, nosotros volition instruct same result, but on dissimilar way.
var str = "JavaScript"
alert(str.substr(0, 4)); // this volition likewise render "Java"
Though this code likewise returns Java, but hither logic is different, equally hither starting index is null together with length of substring is 4, which agency 4 characters long string, which starts from offset character, that's why "Java". If nosotros apply dissimilar declaration nosotros volition instruct dissimilar consequence e.g.
var str = "JavaScript"
alert(str.substr(4, 6)); // this volition render Script
alert(str.substring(4, 6)); // this volition render "Sc"
Here, y'all tin clearly run into that both substr and substring are returning dissimilar result, because starting index is non zero. Because of this many spider web developer often confuse together with they think that substr and substring deport identically, which is non true. You should live real careful, spell using substr() and substring(), otherwise it volition exercise difficult to honour subtle bugs. There is to a greater extent than or less other method called slice() which tin likewise live used for creating smaller substring from String inwards JavaScript, together with y'all tin think of it to avoid whatsoever confusion betwixt substring together with substr in JavaScript.
That's all well-nigh difference betwixt substr together with substring method inwards JavaScript, simply recollect that divergence is inwards the mo argument. The mo declaration of substring method takes terminate index, which is excluded from lastly substring, spell mo declaration of substr method takes maximum length of expected substring.
Further Learning
JavaScript Fundamentals past times Liam McLennan
ES6 Javascript: The Complete Developer's Guide
JavaScript: Understanding the Weird Parts