Adarsh Khatri

~ writings

JavaScript Cheatsheet

javascriptprogrammingreferenceweb development
JavaScript Cheatsheet

Array Methods

  • map(): Creates a new array with the results of a function.
let arr = [1, 2];
arr.map((x = x * 2)); // logs [2,4]
  • filter(): Creates a new array with elements that pass a test.
let arr = [1, 2, 3];
arr.filter((x) => x > 1); // logs [2,3]
  • reduce(): Reduces array to a single value using a function.
let arr = [1, 2];
arr.reduce((a, b) => a + b); // logs 3
  • find(): Returns the first element that passes a test
let arr = [1, 2, 3];
arr.find((x) => x > 1); // logs [2] because 2 is greatest first value.
  • findIndex(): Returns the index of the first element that passes a test.
let arr = [1, 2, 3];
arr.findIndex((x) => x > 1); // logs 1, which is index of 2.

Update more later!