
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating Arrays Using JavaScript
There are many ways to create an Array in JavaScript. We'll look at how to create an empty array first using 2 methods.
let myArr = []; let myArr = new Array();
Both of the above lines create an empty array. The JavaScript community always prefers the first method as it is easier to read, type and performs the same task as the second one. You can also populate the array when you're creating it using either of the following 2 notations −
let myArr = ["Mon", "Tue", "Wed", "Thu", "Fri"]; let myArr = new Array("Mon", "Tue", "Wed", "Thu", "Fri");
You can print it on the console using a console.log statement. For example,
console.log(myArr);
This will give the output.
["Mon", "Tue", "Wed", "Thu", "Fri"]
Though not used much, you can also create an array by specifying its length as follows −
let myArr = new Array(5);
This will create an array of size 5.
Advertisements