34.9k views
1 vote
What parameters does Array.unshift() accept?

User Mstringer
by
8.5k points

1 Answer

0 votes

Final answer:

The Array.unshift() method in JavaScript can accept multiple parameters, which are the elements to add to the start of the array, altering the array and returning its new length.

Step-by-step explanation:

Parameters of Array.unshift() Method

The Array.unshift() method in JavaScript accepts any number of parameters, which correspond to the elements you want to add to the beginning of the array.

The Array.unshift() method is used in programming to add one or more elements to the beginning of an array. It accepts multiple parameters, which are the elements to be added to the front of the array. These parameters can be of any data type, such as numbers, strings, objects, or even other arrays.

Here's an example:

let array = [1, 2, 3];

array.unshift(4, 5);

console.log(array); // [4, 5, 1, 2, 3]

In the example above, the unshift() method is used to add the elements 4 and 5 at the beginning of the array, shifting the existing elements to the right.This method modifies the array by inserting the elements at the start and returns the new length of the array. For example, if you have an array like let fruits = ['apple', 'banana'], and you use fruits.unshift('orange', 'pear'), the fruits array would become ['orange', 'pear', 'apple', 'banana'], and the method would return 4, indicating the new array length.

User Faza
by
8.8k points

No related questions found