- 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
Create empty array of a given size in JavaScript
To create an empty array of given size, use the new operator −
var numberArray = new Array(10);
After that, let’s set some values in the array. Following is the code −
Example
var numberArray = new Array(10); console.log("The length="+numberArray.length) numberArray=[10,20,30,40,50,60]; console.log("The array value="); for(var i=0;i<numberArray.length;i++) console.log(numberArray[i]);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo52.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo52.js The length=10 The array value= 10 20 30 40 50 60
Advertisements