Summary of many times, or will be confused, summary more use

slice

The slice() method returns a new array object that is a shallow copy of the array determined by begin and end (not including end). The original array will not be changed. var fruits = ['orange'.'apple'.'banana'.'watermalon'.'durian'];

console.log(fruits.slice(2));
// expected output: Array ["banana"."watermalon"."durian"]

console.log(fruits.slice(2, 4));
// expected output: Array ["banana"."watermalon"]

console.log(fruits);
// expected output: Array ["orange"."apple"."banana"."watermalon"."durian"]

Copy the code
The slice() method extracts a portion of a string and returns a new string. var str1 ='the HelloWorld! ';
var str2 = str1.slice(4, -2);

console.log(str2); // OUTPUT: oWorl
Copy the code

splice

The splice() method modifies an array by deleting or replacing existing elements and returns the modified contents as an array. This method changes the original array. var fruits = ['orange'.'apple'.'banana'.'watermalon'.'durian'];

console.log(fruits.splice(2));
// deleted ["banana"."watermalon"."durian"]
console.log(fruits);
// expected output: Array ["orange"."apple"]
Copy the code

split

English translation: split; The split() method splits a String into an array of strings using the specified delimiter String to split the String into substrings to determine the location of each split. var fruits ="orange, apple, banana, watermalon, durian";

console.log(fruits.split(","));
// expected output: Array ["orange"."apple"."banana"."watermalon"."durian"]
console.log(fruits);
// expected output: "orange, apple, banana, watermalon, durian"
Copy the code

substring

String, returns a substring, intercepting the string var fruits ="orange"The console. The log (fruits. The substring (1, 3)) / / expected output:"ra"
console.log(fruits)
// expected output: "orange"
Copy the code

Conclusion:

slice splice split substring type
Y Y Array (Y/N)
Y Y Y String (Y/N)
Y The original array changes (Y/N)
Y Convert an array (Y/N) to a string special character