Home javascript How to zero an array

How to zero an array




How can you quickly remove all elements from an array that has already been declared previously?

$ arr = null;

removes the array itself, not its contents.

Answer 1, authority 100%

Maybe I didn’t understand something, but:

var array = [1,2,3,4,5];
array = []; //: D

Answer 2, authority 99%

arr.splice (0, arr.length);


arr.length = 0;

Answer 3, authority 29%

I’m just getting started with JS , and I myself came across this forum in search of an answer to the question posed.

It seems like I found it in the arr.shift (); method. This method clears the 0 array cell.

You can make a loop for clearing an array, for example, or maybe there are more efficient ways.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions