What is the purpose of the Array.push() method in JavaScript?
a) To add elements to the end of an array
b) To remove elements from the array
c) To sort the elements in an array
d) To find the length of an array
Answer:
a) To add elements to the end of an array
Explanation:
The Array.push()
method adds one or more elements to the end of an array and returns the new length of the array. It is useful when you need to append data to an existing array.
Example:
var fruits = ["Apple", "Banana"];
fruits.push("Orange");
console.log(fruits); // Output: ["Apple", "Banana", "Orange"]