How to empty an array in JavaScript
Barrios Freddy
Posted on December 11, 2020
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);
// []
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.
💖 💪 🙅 🚩
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.