The difference between substr() and substring() in JavaScript is that substr() is used to extract a specified number of characters from a given string, starting from a specified position, while substring() is used to extract the characters from a string between two specified indices. substr() can accept negative index values, while substring() cannot.
See below example
var str = "Hello world!";
var res = str.substr(1, 4);
console.log(res); // ello
var str = "Hello world!";
var res = str.substring(1, 4);
console.log(res); // ell
Leave a Reply