Categories
JavaScript Answers

How to add new array elements at the beginning of an array in JavaScript?

Spread the love

To add new array elements at the beginning of an array in JavaScript, we use the unshift method.

For instance, we write

const a = [23, 45, 12, 67];
a.unshift(11);
console.log(a);

to call a.unshift with 11 to prepend 11 to the a 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.