Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite array in JavaScript

How do I overwrite (or unset and then set) an array? Seems like "array = new_array" doesn't work.

like image 967
Ivar Avatar asked Dec 03 '22 14:12

Ivar


1 Answers

To create an empty array to assign to the variable, you can use the Array constructor:

array = new Array();

Or you can use an empty array literal:

array = [];

If you have several references to one array so that you have to empty the actual array object rather than replacing the reference to it, you can do like this:

array.splice(0, array.length);
like image 80
Guffa Avatar answered Dec 21 '22 11:12

Guffa