Arrays are one of the most commonly used data structures in JavaScript. They are ordered collections of values, where each value is called an element, and each element has a numeric index. Arrays can store elements of any type, including numbers, strings, objects, and even other arrays.
let fruits = ['apple', 'banana', 'cherry'];
Array Methods
push()The push method adds one or more elements to the end of an array and returns the new length of the array.
let fruits = ['apple', 'banana'];
fruits.push('orange');
console.log(fruits); // Output: ['apple', 'banana', 'orange']
pop()The pop method removes the last element from an array and returns that element.
let fruits = ['apple', 'banana', 'orange'];
let lastFruit = fruits.pop();
console.log(fruits); // Output: ['apple', 'banana']
console.log(lastFruit); // Output: 'orange'
shift()The shift method removes the first element from an array and returns that element.
let fruits = ['apple', 'banana', 'orange'];
let firstFruit = fruits.shift();
console.log(fruits); // Output: ['banana', 'orange']
console.log(firstFruit); // Output: 'apple'
unshift()The unshift method adds one or more elements to the beginning of an array and returns the new length of the array.
let fruits = ['banana', 'orange'];
fruits.unshift('apple');
console.log(fruits); // Output: ['apple', 'banana', 'orange']
map()The map method creates a new array populated with the results of calling a provided function on every element in the calling array.