3

I'm having trouble understanding the behaviour of the equality operator in JavaScript. This is what I get when I run the following commands on a browser's console:

new Object() == true  // returns false
new Object() != false // returns true

Now, I don't agree with the fact that an Object should be false (although I have understood why after checked the ECMAScript Language Specification), but what really bothers me is that I get two different results on two equivalent logical expressions.

What's happening?

Cynical
  • 9,328
  • 1
  • 15
  • 30
  • `What's happening?` You're using sloppy equality. Use strict equality instead and things will be much more predictable. (also note that objects are never equal to anything except themselves, loose or strict) – CertainPerformance Nov 23 '18 at 07:00
  • 1
    Its not strict equality and yes Object is not false but its not true either, if you use a boolean constructor , this should behave correctly – Shubham Khatri Nov 23 '18 at 07:02
  • `!=` checks the value. For exapmle `'5' != 5` will return false. – Sudhir Ojha Nov 23 '18 at 07:03
  • But when the documentation says that `0`, `-0`, `null`, `false`, `NaN`, `undefined`, or the empty string are evaluated to `false` and everything else is `true`, shouldn't that include Objects too? – Cynical Nov 23 '18 at 12:02

2 Answers2

1

You linked to this which gives a 10 step list of things to check based on what the left and right-hand sides are.

The left-hand side is an object. The right-hand side is a boolean.

This means that it hits step 10:

Return false.

An object is not equal to true nor is it equal to false.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • So what you mean is that an Object is neither `true` nor `false`, but as Mark Meyer said in another answer, its Boolean conversion is always `true`? – Cynical Nov 23 '18 at 07:54
  • @Cynical — Comparing to true or false is not the same as passing it as an argument to the Boolean constructor function. – Quentin Nov 23 '18 at 07:56
1

According to the spec, both of these should return false (this aligns with common sense to me):

new Object() == true  // false
new Object() == false // false

based on:

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

  2. Return false.

Since they both return false and:

A != B is equivalent to !(A == B).

both of these should be true:

new Object() != true  // true
new Object() != false // true

note: This shouldn't be confused with the truthiness of new Object(). In other words new Object() == true is not the same Boolean(new Object()) == true

connexo
  • 53,704
  • 14
  • 91
  • 128
Mark
  • 90,562
  • 7
  • 108
  • 148
  • I get your point, but can you expand a bit your last note? Why aren't those two statement the same? Shouldn't `new Object` be automatically casted to `Boolean`? – Cynical Nov 23 '18 at 07:56
  • 1
    @Cynical the spec says nothing about casting when the left hand is an object and the right hand is not a String or Number. So it shouldn't be cast — it should just return false. Casting an object to boolean does happen in other contexts however like in an [`if` condition](http://www.ecma-international.org/ecma-262/5.1/#sec-12.5) where you're not testing equality but whether the object is *truthy*. – Mark Nov 23 '18 at 08:00