Sunday, 23 July 2017

How to insert an item into an array at a specific index using JavaScript.

How to insert an item into an array at a specific index using JavaScript.

Description:

In this example we explain that how to insert an item into an array at a specific index using JavaScript. How to push an item into array at a specific index in JavaScript array. Or how to insert an item into JavaScript array at a specific index.

So in JavaScript provide the splice function to insert or push an item at a specific index into an array using JavaScript.

Below is the example that demonstrate how to push an item into array at specific index using JavaScript.
Code:

Syntax is: arr.splice (index, 0, item)

var arrFruit = [];
arrFruit[0] = "Banana";
arrFruit[1] = "Apple";
arrFruit[2] = "Orange";
arrFruit[3] = "Blackberry";

console.log(arrFruit.join());
arrFruit.splice(2, 0, " Apricot ");
console.log(arrFruit.join());



0 comments:

Post a Comment