0

What is the difference between creating an array with Array(0) and array = []?

To my knowledge both are empty Array objects.

array
>>> []
Array(0)
>>> []

But when I compare them they return 'false'.

var array = []
array === Array(0)
>>> false

What's going on here??

3
  • @tkim90, I'm guessing that because there's a 0 within Array(0), that's considered the first element, as opposed to the empty [] which always signifies empty arrays? Commented Sep 4, 2017 at 20:21
  • 4
    That's because [] === [] => false Commented Sep 4, 2017 at 20:21
  • @user273072545345 Array(0) means an empty array not an array with 0 as first element. Commented Sep 4, 2017 at 20:22

4 Answers 4

2

To my knowledge both are empty Array objects.

They are

But when I compare them they return 'false'.

When you compare two objects in JavaScript you are testing to see if they are the same object, not if they are identical objects.

Sign up to request clarification or add additional context in comments.

Comments

1

Because arrays are objects, not primitives :

var x = 5; // primitive
var y = 5; // primitive
console.log(x == y); // True
var x = [5]; // object
var y = [5]; // object
console.log(x == y); // False
var x = '5'; // primitive
var y = '5'; // primitive
console.log(x == y); // True
var x = {0:5}; // object
var y = {0:5}; // object
console.log(x == y); // False

About the difference, Check this question's answers (The accepted and the second one) : What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
And more on primitives and objects : object vs. primitive

Comments

1
var p = Array(0); // []
[] var p = []   // []

Both work the same way to create an empty array.

var p = Array(3); //[undefined, undefined, undefined]

Which you can also do using :

var p = [undefined, undefined, undefined];

Again both work the same way internally but have different use cases :

For ex. If you want to create an array with n elements :

var array = new Array(n) // [undefined * n]

If you want to initialise values of arrays while creation :

var arry = [1,2,3,4];

One thing to note here is you can create an initialized array with new Array() as well :

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

But when you try to create an array with one initialised value it takes that one parameter and create an array of that size :

var p = new Array(4)  // [undefined*4]

3 Comments

The last example can be done with var a = new Array(1,2,3,4); as well.
yes you can do that as well what if you want to create an array with one element in same way. for ex: var p = new Array(4) this will create array with 4 values as undefined.
Yes, that's a difference.
1

2 arrays (even empty) can't be compared this way and be equal.

Here's the answer how to compare 2 arrays in a proper way: How to compare arrays in JavaScript?

1 Comment

thank you for the link, very helpful

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.