Type something to search...

JavaScript Arrays

JavaScript Arrays

Arrays are used to store multiple values in a single variable.

Creating an Array

You can create an array using square brackets []:

let fruits = ["Apple", "Banana", "Cherry"];

Accessing Array Elements

Array elements are accessed using their index (starting from 0):

console.log(fruits[0]); // Output: Apple
console.log(fruits[1]); // Output: Banana

Common Array Methods

  1. push: Add an element to the end of the array.
  2. pop: Remove the last element from the array.
  3. length: Get the number of elements in the array.

Example:

fruits.push("Orange");
console.log(fruits); // Output: ["Apple", "Banana", "Cherry", "Orange"]
console.log(fruits.length); // Output: 4

Next: Objects →