
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to allocate memory to an object whose length is set to 0 - JavaScript?
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
- Related Articles
- How to allocate memory in Javascript?
- What happens when length of object is set to 0 - JavaScript?
- How to set dynamic property keys to an object in JavaScript?
- How to get the length of an object in JavaScript?
- How to set default values when destructuring an object in JavaScript?
- How to set JavaScript object values dynamically?
- Convert set to object - JavaScript?
- How to set a String as a key for an object - JavaScript?
- Program to find length of longest sublist whose sum is 0 in Python
- Adjacent elements of array whose sum is closest to 0 - JavaScript
- Set JavaScript object values to null?
- How to determine if JavaScript object is an event?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- How to invert an object in JavaScript?
- How to clone an object in JavaScript?

Advertisements