Let’s say the following is our array object −
var arrayObject = [ "John", "David", "Mike" ]
Use length property to set the length to 0 and clear memory.
The syntax is as follows to clear memory and allocate again −
yourArrayObjectName.length=0; // To clear memory yourArrayObjectName.length=4; // To allocate memory
Example
Following is the code −
var arrayObject = [ "John", "David", "Mike" ] arrayObject.length = 0; console.log(arrayObject); arrayObject.length = 5; for (var i = 0; i < arrayObject.length; i++) console.log(arrayObject[i]);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo277.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo277.js [] undefined undefined undefined undefined undefined