Problem Statement
Which method adds an element to the end of an array?
Explanation
The push method adds one or more elements to the end of an array and returns the new length.
Push modifies the original array directly. It does not create a new array.
You can push multiple elements at once by passing multiple arguments.
The opposite of push is pop, which removes the last element.
Push is one of the most commonly used array methods in JavaScript.
Code Solution
SolutionRead Only
const fruits = ['apple', 'banana'];
// Push single element
fruits.push('orange');
console.log(fruits); // ['apple', 'banana', 'orange']
// Push multiple elements
fruits.push('grape', 'mango');
console.log(fruits); // ['apple', 'banana', 'orange', 'grape', 'mango']
// Push returns new length
const newLength = fruits.push('kiwi');
console.log(newLength); // 6
// Other array methods
const numbers = [1, 2, 3];
numbers.pop(); // Removes last element (3)
numbers.unshift(0); // Adds to beginning
numbers.shift(); // Removes first element
console.log(numbers); // [1, 2]