Array Splice in JavaScript

Level 275 keystrokes

splice() changes array contents by removing or replacing elements.

JavaScript code

// Create an array with five numbers
1let nums = [1, 2, 3, 4, 5];
// splice(start, deleteCount, ...items)
// At index 2, remove 1 element (3) and insert "a" and "b"
2nums.splice(2, 1, "a", "b");
// Print the modified array [1, 2, "a", "b", 4, 5]
3console.log(nums);

Output

[ 1, 2, 'a', 'b', 4, 5 ]
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • ,× 7
  • "× 4
  • ;× 3
  • .× 2
  • (× 2
  • )× 2
  • [× 1
  • ]× 1
  • =× 1

More JavaScript examples at level 2

← JavaScript typing practice