Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JavaScript Algorithms and Data Structures
- ES6
- Use the Spread Operator to Evaluate Arrays In-Place
- ES6 introduces the spread operator, which allows us to expand arrays and other expressions in places where multiple parameters or elements are expected.
- The ES5 code below uses apply() to compute the maximum value in an array:
- var arr = [6, 89, 3, 45];
- var maximus = Math.max.apply(null, arr);
- maximus would have a value of 89.
- We had to use Math.max.apply(null, arr) because Math.max(arr) returns NaN. Math.max() expects comma-separated arguments, but not an array. The spread operator makes this syntax much better to read and maintain.
- const arr = [6, 89, 3, 45];
- const maximus = Math.max(...arr);
- for more:https://www.file-upload.com/l60a3g41aj2s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement