JavaScript(JS) array method - unshift

The unshift() method in JavaScript is used to add one or more elements to the beginning of an array. It modifies the original array and returns the new length of the array.

Here is an example of using the unshift() method:

‮refer‬ to:theitroad.com
const fruits = ['apple', 'banana', 'cherry'];
const length = fruits.unshift('orange', 'pear');
console.log(fruits); // ["orange", "pear", "apple", "banana", "cherry"]
console.log(length); // 5

In the example above, the unshift() method is used to add two elements, "orange" and "pear", to the beginning of the fruits array. The resulting array is ["orange", "pear", "apple", "banana", "cherry"], and the new length of the array is 5.

Note that the unshift() method modifies the original array. If you want to add elements to an array without modifying the original array, you can use the concat() method or the spread syntax (...) to create a new array that includes the new elements.