Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Remove a Specific Element from an Array in JavaScript</title> </head> <body> <script> var colors = ["Red", "Green", "Blue", "Yellow", "Orange"]; var removed = colors.splice(2,1); // Removes the third element console.log(colors); // Prints: ["Red", "Green", "Yellow", "Orange"] console.log(removed); // Prints: ["Blue"] (one item array) console.log(removed.length); // Prints: 1 var persons = ["Alice", "John", "Peter", "Clark", "Harry"]; removed = persons.splice(2,2); // Removes the third and fourth elements console.log(persons); // Prints: ["Alice", "John", "Harry"] console.log(removed); // Prints: ["Peter", "Clark"] console.log(removed.length); // Prints: 2 var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"]; removed = fruits.splice(2); // Removes all elements starting at index 2 console.log(fruits); // Prints: ["Apple", "Banana"] console.log(removed); // Prints: ["Mango", "Orange", "Papaya"] console.log(removed.length); // Prints: 3 </script> <p><strong>Note:</strong> Please check out the browser console by pressing the f12 key on the keyboard.</p> </body> </html>