Array Fill in JavaScript

Level 281 keystrokes

fill() fills array elements with a static value, optionally within a range.

JavaScript code

// Create a new array with 5 slots, all filled with 0
1let arr = new Array(5).fill(0);
// Print the array [0, 0, 0, 0, 0]
2console.log(arr);
// fill(value, start, end) fills from index 2 to 4 (exclusive) with 1
// Result: [0, 0, 1, 1, 0]
3console.log(arr.fill(1, 2, 4));

Output

[ 0, 0, 0, 0, 0 ]
[ 0, 0, 1, 1, 0 ]
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.

  • (× 5
  • )× 5
  • .× 4
  • ;× 3
  • ,× 2
  • =× 1

More JavaScript examples at level 2

← JavaScript typing practice