JSFiddle - React, Tailwind, and code Playground

by Pankaj Kargirwar

JavaScript

let myArray = [1, 2, 3, 4, 5];

for (let i = 0; i < myArray.length; i++) {
    if (myArray[i] === 3) {
        // Insert a new element after the element with value 3
        myArray.splice(i + 1, 0, 6, 7);
    }
    
    console.log(`i: ${i} ${myArray[i]}`);
}

console.log(myArray); // Output: [1, 2, 3, 6, 7, 4, 5]
console.log(myArray.length); // Output: 7