Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ",,," == Array(4) in Javascript?

Tags:

javascript

People also ask

Does == work for arrays in JavaScript?

Arrays are objects in JavaScript, so the triple equals operator === only returns true if the arrays are the same reference.

Why array is not equal to array in JS?

However, we cannot use the equality operator for comparing the arrays. The reason behind this is that JavaScript array is an object type and objects are compared based on the references of the variables and not on the values. In the above code, we have initialized two arrays that are the same.

What does array () mean in JavaScript?

In JavaScript, an array is an ordered list of values. Each value is called an element specified by an index. An JavaScript array has the following characteristics: First, an array can hold values of mixed types. For example, you can have an array that stores elements with the types number, string, and boolean.

How do you define an array called array1 in JavaScript?

An array is a special type of variable that stores multiple values using a special syntax. An array can be created using array literal or Array constructor syntax. Array literal syntax: var stringArray = ["one", "two", "three"];


Because the right hand operand is converted to a string and the string representation of Array(4) is ,,,:

> Array(4).toString()
  ",,,"

If you use the array constructor function and pass a number, it sets the length of the array to that number. So you can say you have four empty indexes (same as [,,,]) and the default string representation of arrays is a comma-separated list of its elements:

> ['a','b','c'].toString()
  "a,b,c"

How the comparison works is described in section 11.9.3 of the specification. There you will see (x == y):

8. If Type(x) is either String or Number and Type(y) is Object,
return the result of the comparison x == ToPrimitive(y).

(arrays are objects in JavaScript)

and if you follow the ToPrimitive method you will eventually find that it it calls toString.


Internally its going

",,," == Array(4).toString()

Try using ===. When using == in Javascript, it will attempt to cast the variables, thus leading to issues like this one. The console is casting Array(4) to the string representation (i.e. Array(4).toString), which is ",,,". The reason the commas are there is that the .toString() function adds them to separate items in an array.

See the snippet below:

document.write( Array(4).toString() );

This is because Array(4) initialises an array of 4 empty values, an == implicitly converts, so:

 ",,," == Array(4)

 ",,," == Array(4).toString()

 ",,," == ["", "", "", ""] // note 3 commas for 4 values

 ",,," == ["", "", "", ""].toString()

Are all similar.

== does implicit type conversions before comparing the values, which can result in unpredictable results. Use === to check the type and the value.


Comparing an Array to a string coerces the Array to a string before doing the comparison. Coercing an empty 4-element Array to a string yields that exact string.


I first thought it was something with the "prototype"... but after a little investigation I reached a sad conclusion...

Apparently it is an internal and more obscure js thing with not much logic...

Just try

Array(4)==Array(4)

and no coercion on types also...

Array(4)===Array(4)

and you'll get FALSE

you know that null==null, null===null and even undefined==undefined and undefined===undefined returns TRUE... so... it's a bit obscure...

Array(4)==[,,,] should be true also