- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements