Arrays & Methods

Duration: 15 min  •  Difficulty: Medium

Arrays store sequences of items. The magic of JavaScript: array contents can even be a mix of various unrelated data types!

Declaration

main.js
const fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Output: Apple

Modern Array Methods

  • push(): Adds an item at the very end.
  • map(): Transforms/maps each array cell to a new cell arrangement (the heart of HTML mapping logic in ReactJS).
  • filter(): Removes elements if the condition is false.
  • main.js
    const numbers = [1, 2, 3, 4, 5];
    // Map: Multiply all numbers in the array by two
    const doubled = numbers.map(num => num * 2);
    console.log(doubled); // [2,4,6,8,10]
    // Filter: Extract all even numbers
    const evens = numbers.filter(num => num % 2 === 0);
    console.log(evens); // [2,4]