Yogesh Chauhan's Blog

Avoid using new Array() in JavaScript

in JavaScript on December 7, 2020

We saw 2 ways to create arrays in JavaScript:

2 Ways We Can Create An Array In JavaScript

We also looked at the better method.

In this article, I’ll discuss the example why it’s not a good idea to use new array() method to create arrays.

Two different ways to create numbers array:


var numbers = new Array();     
var numbers = []; 

Let’s create the arrays with some numbers in it:


var numbers = new Array(1,2,3,4);     
var numbers = [1,2,3,4]; 

Its fine until this point. 

Let’s access elements.


var numbers = new Array(1,2,3,4);   
console.log(numbers[1]); //2
console.log(numbers.length);  //4

var numbers = [1,2,3,4]; 
console.log(numbers[1]); // 2
console.log(numbers.length);  //4

Seems perfectly fine. Right?

What if we want to create an array of prime numbers but add the numbers one by one.

Let’s add just one number “2” for now and add some numbers later on.


var numbers = new Array(2);   
console.log(numbers[0]); //undefined
console.log(numbers.length);  //2

var numbers = [2]; 
console.log(numbers[0]); // 2
console.log(numbers.length);  //1

It messed up the whole plan. Didn’t it?

📌 So, when we try to create a single element array with new array(), it created the array of the size of that element but keeps the elements empty inside it.

If you are really determined to use that method then you might argue to add 2 elements and then adding other elements later on.


var numbers = new Array(2,2);   
console.log(numbers[0]); //2
console.log(numbers.length);  //2

var numbers = [2,2]; 
console.log(numbers[0]); // 2
console.log(numbers.length);  //2

Yay! We did it! ✌

But Prime numbers are 2, 3, 5, 7, 11, .. and you entered a wrong number in it. So, now you would want to remove it.

Let’s try to change some elements in it.


var numbers = new Array(2,2);   

numbers.pop();
numbers.push(3);

console.log(numbers[1]); //3
console.log(numbers.length);  //2

var numbers = [2,2]; 

numbers.pop();
numbers.push(3);

console.log(numbers[1]); //3
console.log(numbers.length);  //2

Works just fine.

So, if you plan ahead and keep the problem in mind, you might avoid the problem.

But if you’re going to create an array using new array() method, you might run into problems.

Like this example,


var numbers = new Array(2);   

numbers.push(3);

console.log(numbers[1]); //undefined
console.log(numbers.length);  //3

var numbers = [2]; 

numbers.push(3);

console.log(numbers[1]); //3
console.log(numbers.length);  //2

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Advanced Array Methods in JavaScript (with examples)JavaScriptList of WordPress directories functionsWordPressHow to Hide HTML elements automatically after few seconds using JavaScript?HTMLHow to send and receive query strings via links in Angular 9?AngularHow to make a UILabel clickable in Swift 5?SwiftHow to hide a DIV on clicks outside of it using jQuery?jQuery