## [] == ![] evaluates to true

false

why is false

javascript false

[] === []

false

false javascript

javascript array length 0 but not empty

I would like to know why the expression given in the title

`[] == ![]`

is evaluated to `true`

.

You cannot compare arrays as strings. I get that. If

`[] == []`

will evaluate to false because the references are different. Though if we have the following statement.

var arr = []; arr == arr // this evaluates to true simply because references are the same.

In order A == B to return `true`

either A and B have to be `false`

or `true`

.
A == !B in order to return `true`

A can be `true`

and B can be `false`

or vice versa but in this case, A and B are the same values so I don't get it.

`![]`

evaluates to `false`

because the reference is truthy. `[]`

can be converted to an number (0 in this case) which is falsey. Therefore: the condition passes as equal. If you did `===`

it would be false.

**javascript - [] == ![] evaluates to true,** I would like to know why the expression given in the title. [] == ![] is evaluated to true . You cannot compare arrays as strings. I get that. If. [] == []. will evaluate to Write an expression that evaluates to true if and only if the string variable s does not equal end. !strcmp(s,"end")==0 Given the string variables name1 and name2, write a fragment of code that assigns the larger of the two to the variable first (assume all three are already declared and name1 and name2 assigned values).

Basically Javascript tries to convert both the sides into Number if both the types are not same. And if its an Object, It tries to convert into primitive value

So in this case step by step will be

=> []==![] => []==false // Type conversion by the statement itself => []==0 // To number of right operand => ""==0 // To Primitive call for Array which will in this case convert to empty string => 0==0 // To number call of "" which is 0 => true

One can check for the ecmascript explanation here in the compiler description http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

**What are truthy values in JavaScript?,** Any primitive type will evaluate to true in JavaScript, with the exception of the following Most values are truthy in JavaScript, including the empty array [] and the to true in a boolean context, they do not loosely equal ( == ) the boolean value that bool, true, and false be always defined in C99, and modules that use definitions incompatible with this could #include <nostdbool.h> (or maybe a #define or #pragma) to escape from this.

Whenever 2 values are compared using == , javascript performs the Abstract Equality Comparison Algorithm.

Here, x is [], and y is ![]. Also,

typeof([]) // "object" typeof(![]) // "boolean"

Since y is a boolean and x is an object, condition 7 is the first to hold:

If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

What’s the value of ToNumber(y)?

Number(![]) // 0

because [] is a truthy value, negating makes it false. Number(false) is 0

Now we have the comparison: [] == 0.

Since typeof(0) is "number", condition 8 now holds:

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

ToPrimitve(x) is like x.toString().

[].toString() // "" - the empty string

Almost done we now face with the comparison: "" == 0

Now, condition 5 holds:

If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

ToNumber("") // 0

Finally both operands have the same type and condition 1 holds. I think you can take if from here :)

read about Abstract Equality Comparison on the specs!

**JavaScript,** Last week I posted an article: JavaScript: Can (a==1 && a==2 && a==3) ever evaluate to true? The answer is yes, it can! And there are actually To permit more than one statement to execute if an expression evaluates to true, C++ provides a structure called a ____ statement.

Here is a more detail explanation from codementor.io

Before understanding what is happening , we need to understand the concept of truthy and falsy in JavaScript and how the ! (logical NOT) operator works. Values such as * false, null, undefined, NaN, 0, '' and ""* are considered as falsy.
Other values like

`*true, {}, [], "foo"`

* etc. are considered truthy.
The ! operator, on the other hand, is defined for boolean values only.
Any other data type will automatically be coerced into it's corresponding boolean value when operated with ! operator.
Here, `![]`

evaluates to false and the comparison actually becomes `'[] == false'`

which evaluates to `'true'`

.
Isn't that supposed to be false, since empty arrays are truthy?
That's right but the double equal operator evaluates expressions on certain rules.
We're trying to compare an object with a boolean value and JavaScript will implicitly convert the operands to Number type. `Number([]) is 0`

and `Number(false) is also 0`

, which evaluates to `true since zero is equal to zero`

**Truthy and Falsy: When All is Not Equal in JavaScript,** 0] == true; // all false false == null; NaN == NaN; Infinity == true; [] == true; [0] == true; runs if x is false, 0, '', or [] // use if (!x) // // runs if x is Let T(i, j) represents the number of ways to parenthesize the symbols between i and j (both inclusive) such that the subexpression between i and j evaluates to true. <!– –> Let F(i, j) represents the number of ways to parenthesize the symbols between i and j (both inclusive) such that the subexpression between i and j evaluates to false

Your Understanding is correct but you have missed that ! operator is asking for explicit coercion of empty array to Boolean.

Steps in [] == ![]

- Explicit Coercion right side expression to Boolean by ! operator. So , ![] becomes false.
- Now action of == operator. Since one of them is Boolean ( false on right side of == operator). which will coerced to Number.
- Where as [] will be coerced to Number which 0.

So 0 == 0 , which is true.

**[] == ![] - WTFJS And Coercion - DEV Community ,** Since [] isn't falsy, it'll evaluate to true . the ! will make it false , so we is empty we get 0 . Now. 0 == 0 . That's true. That's why [] == ![] is true . The body of a while loop will be continually executed until the continuation condition evaluates to True. False. When one loop is contained in the body of another

**Advanced Javascript: Logical Operators and truthy / falsy,** if ( [] == false ) { // this code runs } if ( [] ) { // this code also runs } if ([] == true) { // this code doesn't run } if ( ![] ) { // this code also doesn't run } (However the string “false” is evaluated as truthy by both PHP and javascript.) <?php $emptyArray Assuming the value of a is 2 and the value of b is 3, then state whether this statement evaluates to True or False: (aa<b) or not(aa<a) True.

**Truth, Equality and JavaScript – JavaScript, JavaScript…,** 3. 4. if ([0]) {. console.log([0] == true ); //false. console.log(!![0]); //true. } String, In effect evaluates Number(string) “abc” -> NaN “123” -> 123. Start studying Final Exam C++ Part 2. Learn vocabulary, terms, and more with flashcards, games, and other study tools.

**How to Avoid Common Pitfalls in JavaScript,** Here, ![] evaluates to false and the comparison actually becomes: > [] == false true. Isn't that supposed to be false, since empty arrays are truthy First, looking at the two conditions, '\0' is a constant of type integer, which denotes the null character C, which is the same as 0.While "\0" is a string literal, which contains 2 bytes, the one specified and the null terminator byte implicitly added.

##### Comments

- stackoverflow.com/a/9550412/19068 has a whole list of questions asking why X == Y (or != Y).
- Note from the tour:
*Focus on questions about an actual problem you have faced.* *"Because the reference is truthy"*— a reference isn’t truthy or not truthy, a*value*is.*"Therefore: the condition passes as equal"*— it doesn’t pass as equal because something falsy is compared with`false`

using abstract comparison (or else`NaN == false`

,`null == false`

, etc. would be`true`

); you’re missing the type coercion for`false`

. Step by step,`[]`

is coerced to`""`

, then to`0`

, then`false`

is coerced to`0`

, then strict equality comparison is used as`0 === 0`

, resulting in`true`

.- This should not be the chose answer, as it does not detail the implicit type coercion happening on both sides of the equality operator, as explained by both @AkarshSatija and SebastianSimon.