Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Add or Remove Array Elements at any Index</title> </head> <body> <script> let colors = ["Red", "Green", "Blue"]; let removed = colors.splice(0,1); // Remove the first element document.write(colors + "<br>"); // Prints: Green,Blue document.write(removed + "<br>"); // Prints: Red (one item array) document.write(removed.length + "<br>"); // Prints: 1 removed = colors.splice(1, 0, "Pink", "Yellow"); // Insert two items at position one document.write(colors + "<br>"); // Prints: Green,Pink,Yellow,Blue document.write(removed + "<br>"); // Empty array document.write(removed.length + "<br>"); // Prints: 0 removed = colors.splice(1, 1, "Purple", "Voilet"); // Insert two values, remove one document.write(colors + "<br>"); //Prints: Green,Purple,Voilet,Yellow,Blue document.write(removed + "<br>"); // Prints: Pink (one item array) document.write(removed.length); // Prints: 1 </script> </body> </html>