Run

Tutorial With Example

Run your live code

 
​x
 
1
<html>
2
<head></head>
3
<body>
4
  <script>
5
  var colors = ["Red", "Green", "Blue"];
6
  var removed = colors.splice(0,1); // Remove the first element
7
​
8
  document.write(colors); // Prints: Green,Blue
9
  document.write(removed); // Prints: Red (one item array)
10
  document.write(removed.length+"<br>"); // Prints: 1
11
​
12
  removed = colors.splice(1, 0, "Pink", "Yellow"); // Insert two items at position one
13
  document.write(colors); // Prints: Green,Pink,Yellow,Blue
14
  document.write(removed); // Empty array
15
  document.write(removed.length+"<br>"); // Prints: 0
16
​
17
  removed = colors.splice(1, 1, "Purple", "Voilet"); // Insert two values, remove one
18
  document.write(colors); //Prints: Green,Purple,Voilet,Yellow,Blue
19
  document.write(removed); // Prints: Pink (one item array)
20
  document.write(removed.length+"<br>"); // Prints: 1
21
  </script>
22
</body>
23
</html>