JavaScript Arrays
What is an Array?
An array is a special variable, which can hold more than one value at a time. It is a collection of elements stored in a single variable.
Creating an Array
There are multiple ways to create an array in JavaScript:
-
Using an array literal:
let fruits = ["Apple", "Banana", "Cherry"]; console.log(fruits); // ["Apple", "Banana", "Cherry"] -
Using the
Arrayconstructor:let fruits = new Array("Apple", "Banana", "Cherry"); console.log(fruits); // ["Apple", "Banana", "Cherry"]
Accessing Array Elements
You can access elements of an array by their index:
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // "Apple"
console.log(fruits[1]); // "Banana"
console.log(fruits[2]); // "Cherry"
Modifying Array Elements
You can modify elements of an array by their index:
let fruits = ["Apple", "Banana", "Cherry"];
fruits[1] = "Blueberry";
console.log(fruits); // ["Apple", "Blueberry", "Cherry"]
Array Methods
-
push()Adds new elements to the end of an array:
let fruits = ["Apple", "Banana"]; fruits.push("Cherry"); console.log(fruits); // ["Apple", "Banana", "Cherry"] -
pop()Removes the last element of an array:
let fruits = ["Apple", "Banana", "Cherry"]; let lastFruit = fruits.pop(); console.log(fruits); // ["Apple", "Banana"] console.log(lastFruit); // "Cherry" -
shift()Removes the first element of an array:
let fruits = ["Apple", "Banana", "Cherry"]; let firstFruit = fruits.shift(); console.log(fruits); // ["Banana", "Cherry"] console.log(firstFruit); // "Apple" -
unshift()Adds new elements to the beginning of an array:
let fruits = ["Banana", "Cherry"]; fruits.unshift("Apple"); console.log(fruits); // ["Apple", "Banana", "Cherry"] -
lengthReturns the length of an array:
let fruits = ["Apple", "Banana", "Cherry"]; console.log(fruits.length); // 3 -
concat()Joins two or more arrays:
let fruits = ["Apple", "Banana"]; let moreFruits = ["Cherry", "Date"]; let allFruits = fruits.concat(moreFruits); console.log(allFruits); // ["Apple", "Banana", "Cherry", "Date"] -
slice()Returns selected elements in an array, as a new array:
let fruits = ["Apple", "Banana", "Cherry", "Date"]; let someFruits = fruits.slice(1, 3); console.log(someFruits); // ["Banana", "Cherry"] -
splice()Adds/removes elements from an array:
let fruits = ["Apple", "Banana", "Cherry"]; fruits.splice(1, 1, "Blueberry", "Kiwi"); console.log(fruits); // ["Apple", "Blueberry", "Kiwi", "Cherry"] -
forEach()Calls a function once for each array element:
let fruits = ["Apple", "Banana", "Cherry"]; fruits.forEach(function(fruit) { console.log(fruit); }); // Output: // "Apple" // "Banana" // "Cherry" -
filter()Creates a new array with elements that pass a test:
let numbers = [1, 2, 3, 4, 5]; let evens = numbers.filter(n => n % 2 === 0); console.log(evens); // [2, 4] -
reduce()Reduces an array to a single value:
let numbers = [1, 2, 3, 4]; let sum = numbers.reduce((acc, curr) => acc + curr, 0); console.log(sum); // 10 -
find()andfindIndex()Returns the first element or index that matches a condition:
let users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; console.log(users.find(u => u.id === 2)); // { id: 2, name: 'Bob' } console.log(users.findIndex(u => u.id === 2)); // 1 -
includes()Checks whether an array contains a value:
let fruits = ['Apple', 'Banana']; console.log(fruits.includes('Banana')); // true -
sort()andreverse()let nums = [3, 1, 4, 1, 5]; nums.sort((a, b) => a - b); console.log(nums); // [1, 1, 3, 4, 5] nums.reverse(); console.log(nums); // [5, 4, 3, 1, 1] -
every()andsome()let scores = [80, 90, 85]; console.log(scores.every(s => s >= 60)); // true console.log(scores.some(s => s >= 90)); // true
Spread and Destructuring with Arrays
let arr1 = [1, 2];
let arr2 = [...arr1, 3, 4]; // [1, 2, 3, 4]
let [first, second, ...rest] = [10, 20, 30, 40];
console.log(first, second, rest); // 10 20 [30, 40]
Multidimensional Arrays
let matrix = [[1, 2], [3, 4]];
console.log(matrix[1][0]); // 3