1. They both take two arguments, slice and substring take the start and end position (excluding the end position), and substr takes the start position and the length of the string to return. In addition, substring starts with the smaller of the two arguments and ends with the larger argument.
var test = 'hello world';
console.log(test.slice(4.7)); //o w
console.log(test.substring(4.7)); //o w
console.log(test.substr(4.7)); //o world
console.log(test.slice(7.4)); // Empty string console.log(test.substring(7.4)); //o w
Copy the code
2. When a negative parameter is received, Slice adds the length of its string to the corresponding negative number, using the result as an argument. Substr simply takes the result of the first argument plus the length of the string as the first argument; Substring simply converts all negative arguments to 0.
var test = 'hello world';
console.log(test.slice(- 3)); //rld
console.log(test.substring(- 3)); //hello world
console.log(test.substr(- 3)); //rld
console.log(test.slice(3.4 -)); //lo w
console.log(test.substring(3.4 -)); //hel
console.log(test.substr(3.4 -)); // An empty stringCopy the code