YogeshChauhan . com

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
amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

OOP, Class and Objects Strategies For Beginners (PHP)PHPContent Blocks in SCSS (Sass)SCSSWhat is Conditional Rendering in React?ReactColumn and Table Alias in PostgresPostgresNATURAL JOIN in PostgresPostgresHow to use @if and @else in SCSS?SCSS