Streaming on Twitch twice a week

Quickly removing values from an array


Setting the length of an array in JavaScript removes all values beyond that length. Setting the length to zero removes all values from it.

// this array holds six values
const numbers = [4, 8, 15, 16, 23, 42]

// by setting the length to 4, any value past the fourth element is removed
numbers.length = 4
console.log(numbers)  // ⇒ [4, 8, 15, 16]

// setting the length to 0 leads to the entire array being emptied
numbers.length = 0
console.log(numbers)  // ⇒ []

More fire tips

Splitting arrays into chunks

By combining map and slice, we can write a helper function to split large arrays into many similarly sized blocks. This is useful for features like pagination.

Read fire tip

Read all fire tips →