How to add item to an array at a specific index in JavaScript

3 Answers

0 votes
const arr = ['php', 'c++'];

arr.splice(1, 0, 'javascript');

document.write(arr);

 
 
/*
run:
       
php,javascript,c++ 
     
*/

 



answered Nov 5, 2019 by avibootz
0 votes
const arr = ['php', 'c++', 'c#'];

arr.splice(1, 0, 'javascript', 'python');

document.write(arr);

 
 
/*
run:
       
php,javascript,python,c++,c# 
     
*/

 



answered Nov 5, 2019 by avibootz
0 votes
const arr = ['php', 'c++', 'c#'];

arr.splice(0, 0, 'javascript');

document.write(arr);

 
 
/*
run:
       
javascript,php,c++,c# 
     
*/

 



answered Nov 5, 2019 by avibootz

Related questions

...