Introduction
Sometimes, we may wish to insert an item into an array at a specific index in JavaScript. In this post, we'll learn how to do it.
How to insert an item into an array at a specific index in JavaScript
To insert an item into an array at a specific index in JavaScript you need to use the splice()
function.
In the following example, we will create an array and add elements to it. Then we will use splice(2, 0, "CCCCCCCC")
function to insert element on index 2
with value "CCCCCCCC":
Conclusion
In this short article, we described how to insert an item into an array at a specific index in JavaScript.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}