What is the difference between substr and substring in JavaScript?
For example, what distinguishes the following two statements:
alert(“abc”.substr(0, 2)); and alert(“abc”.substring(0, 2)); They both seem to output “ab.” What are the key differences between javascript substr vs substring?
substr: The first parameter specifies the starting index, and the second parameter specifies the number of characters to extract.
For example:
alert("abc".substr(0, 2)); // Outputs: "ab"
substring: The first parameter specifies the starting index and the second parameter specifies the ending index (non-inclusive). If the second parameter is omitted, it extracts till the end of the string.
For example:
alert("abc".substring(0, 2)); // Outputs: "ab"