Categories
JavaScript Answers

How to insert an item into an array at a specific index with JavaScript?

Spread the love

To insert an item into an array at a specific index with JavaScript, we can assign the value directly.

For instance, we write

const arr = [];
arr[0] = "Jane";
arr[1] = "Alex";
arr[2] = "Bob";

to set the entry at index 0 to 'Jane'.

We set the entry at index 1 to 'Alex'.

And we set the entry at index 2 to 'Bob'.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.