Explore Courses Blog Tutorials Interview Questions
0 votes
in Web Technology by (47.6k points)

I am looking for a JavaScript array insert method, in the style of:

arr.insert(index, item)

Preferably in jQuery, but any JavaScript implementation will do at this point.

1 Answer

0 votes
by (106k points)
edited by

To insert an item into an array at a specific index in JavaScript you can use the splice function.

arr.splice(index, 0, item); 

This will insert item into array at the specified index by deleting 0 items first, that is, it's just an insert.

See the example below:-

var arr = ["Jani", "Hege","Stale","Kai”,Jim","Borge"]; 

arr.splice(2, 0, "Lene"); 


Check out Web Development Online Courses and enroll today! 

Related questions

0 votes
1 answer
0 votes
1 answer

Browse Categories