Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Insert a Value into an Array at the Specified Index in JavaScript</title> </head> <body> <script> var persons = ["Harry", "Clark", "John"]; // Insert an item at 1st index position persons.splice(1, 0, "Alice"); document.write(persons); // Prints: Harry,Alice,Clark,John document.write("<hr>"); // Insert multiple elements at 3rd index position persons.splice(3, 0, "Ethan", "Peter"); document.write(persons); // Prints: Harry,Alice,Clark,Ethan,Peter,John </script> </body> </html>