Categories
JavaScript Answers

How to prepend a value to an array with JavaScript?

Spread the love

To prepend a value to an array with JavaScript, we call the unshift method.

For instance, we write

a.slice().unshift(0);

to call a.slice to make copy of the a array.

And then we call unshift on the copied array to prepend 0 into the array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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