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 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to convert an object from API to JSON array in Angular 9?AngularWHERE Clause in PostgresPostgresWhat does Containment mean in React?ReactHow to center an image horizontally and vertically?CSSThe :last-of-type selectorCSSHow to set opacity or transparency using CSS?CSS