Spread operator to the opposite job of Rest parameter.
Below all examples combined together, please check one by one. So that you can understand it easily.
Below all examples combined together, please check one by one. So that you can understand it easily.
// Normal way - will not work let arr = [7, 5, 4]; alert( Math.max(arr) ); // NaN //Could work with spread operator let arr = [7, 5, 4]; alert( Math.max(...arr) ); // 7 (spread turns array into a list of arguments) //Multiple iterables array let arr1 = [1, -2, 6, 5]; let arr2 = [8, 9, -8, 7]; alert( Math.max(...arr1, ...arr2) ); // 9 //Combined let arr1 = [1, -2, 6, 5]; let arr2 = [8, 9, -8, 7]; alert( Math.max(3, ...arr1, 2, ...arr2, 52, 56) ); // 56 //Merging array is easy let arr = [3, 5, 1, 4, 6, 8]; let arr2 = [8, 9, 15, 2, 1, 7, 3]; let merged = [0, ...arr, 2, ...arr2]; alert(merged); // 0,3,5,1,4,6,8,2,8,9,15,2,1,7,3 // String into array of characters let str = "Hello"; alert( [...str] ); // H,e,l,l,o
No comments:
Post a Comment