What will the Array.unshift() method do?

What will the Array.unshift() method do?

a) Add one or more elements to the beginning of an array
b) Remove the first element of an array
c) Add one or more elements to the end of an array
d) Sort the elements of an array

Answer:

a) Add one or more elements to the beginning of an array

Explanation:

The Array.unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

Example:


let fruits = ["Banana", "Cherry"];
fruits.unshift("Apple");
console.log(fruits); // Output: ["Apple", "Banana", "Cherry"]
    

Reference:

JavaScript MCQ (Multiple-Choice Questions)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top