JavaScript Array unshift() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Array.unshift() is a JavaScript method that adds one or more elements to the beginning of an array and returns the new length of the array. It modifies the original array rather than creating a new one. This operation shifts existing elements to higher indexes to accommodate the new elements. It's commonly used when you need to prepend elements to an array, altering its structure dynamically.

If multiple elements are added at once, they get inserted into the array having the same order as provided. But if we add elements one by one like a loop, the result is different.

For Example

Output

Syntax of unshift() Javascript

The following is the syntax of unshift() in javascript:

Here, array is the name of the array variable in which unshift will add an element in the beginning. The unshift JavaScript function accepts one argument (elements).

Parameters of unshift() Method in Javascript

From the syntax, we can see that it takes only one parameter.

  • Element that can take any value. Element is added at the beginning of the array. We could give one or more elements to unshift() in a single call.

Return Value of unshift() in Javascript

Return Type: number

The unshift() function returns the new length of the array after the addition of the element at the beginning of the array.

Exceptions for unshift() Method in Javascript

While unshift() takes any value as an element, there can be few errors caused. One of the most common error is TypeError.

**Type Error:** unshift is not a function - error in javascript. It occurs when the value called is not an array. To solve this error, the value should be converted to an array, or we can call unshift method only on arrays.

Example of TypeError:

Output:

Example(without Error):

If we take array instead of integer in the above code, then the error will go.

Output:

Examples of unshift() in Javascript

In the following example, 30 and 35 are added at the beginning of the array.

Code

Output:

In the above result, we see that the number of elements present in an array after the insertion of elements is shown, and in the next line, the array is printed.

More Examples:

1. Using unshift() Method with an Array of Strings and Integers

Output:

In the above code, first, we add the element to the array of strings using unshift(), and then we add elements to the array of integers using unshift().

Browser Compatibility

Regarding browser compatibility, the unshift() method is supported in all major browsers, including:

  • Google Chrome
  • Mozilla Firefox
  • Safari
  • Microsoft Edge
  • Opera

Conclusion

  • To add an element at the beginning of the array, we use unshift in JavaScript.
  • The unshift() function only work with array variables; otherwise, it will give an error.
  • The inbuilt unshift function accepts only one parameter, which may be a single element or list of elements that we want to add at the front of the array.

See Also