Advertisement
sissou123

Untitled

Mar 16th, 2022
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. JavaScript Algorithms and Data Structures
  2. ES6
  3. Use the Spread Operator to Evaluate Arrays In-Place
  4. ES6 introduces the spread operator, which allows us to expand arrays and other expressions in places where multiple parameters or elements are expected.
  5.  
  6. The ES5 code below uses apply() to compute the maximum value in an array:
  7.  
  8. var arr = [6, 89, 3, 45];
  9. var maximus = Math.max.apply(null, arr);
  10. maximus would have a value of 89.
  11.  
  12. 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.
  13.  
  14. const arr = [6, 89, 3, 45];
  15. const maximus = Math.max(...arr);
  16. for more:https://www.file-upload.com/l60a3g41aj2s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement