How to empty an array in JavaScript

barriosdfreddy

Barrios Freddy

Posted on December 11, 2020

How to empty an array in JavaScript

The simple way to empty an array in JavaScript is through assigning 0 to the property length of the array affected. For Example

let numbers = [1,3,5,7,9];

numbers.length = 0;

console.log(numbers);
// []

Enter fullscreen mode Exit fullscreen mode

This is possible because the length property is read/write property in an array. Therefore, you can assign zero and the array will be clear.

💖 💪 🙅 🚩
barriosdfreddy
Barrios Freddy

Posted on December 11, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to empty an array in JavaScript
javascript How to empty an array in JavaScript

December 11, 2020