1

I always thought that JavaScript's if statements did some kind of casting magic to their arguments, but I'm a little wary of what's actually going on behind the scenes.

I recently found a JavaScript comparison table and noticed that even though -1 == true evaluates to false, if(-1){...} will execute.

So within JavaScripts if statements, what happens to the expression? It seems reasonable to assume that it uses !!{expression} to cast it to an inverse boolean, then invert it again, but if that's the case, how does JS decide whether an object's inverse boolean representation is truthy or not?

4
  • I don't understand -- -1 is truthy, so it will execute. (truthy != true) Commented Mar 28, 2014 at 20:58
  • 3
    there is a difference between something being "truthy" and something being (boolean) true value Commented Mar 28, 2014 at 20:59
  • e.g. var x = true; is both truthy and boolean true. But var x = 'true'; is truthy but 'true'!=true because 'true' is a string type value and true is a boolean type value Commented Mar 28, 2014 at 21:01
  • ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf 11.9.3 The Abstract Equality Comparison Algorithm 7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y). Commented Mar 28, 2014 at 21:05

5 Answers 5

6

JavaScript is wonky.

Yes, -1 == true results in false, but that's not what the if statement is doing. It's checking to see if the statement is 'truthy', or converts to true. In JavaScript, that's the equivalent of !!-1, which does result in true (all numbers other than zero are truthy).

Why?!?

The spec defines the double equals operator to do the following when presented with a number and a boolean:

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

ToNumber will convert the boolean true into the number 1, so you're comparing:

-1 == 1

which anyone can tell you is clearly false.

On the other hand, an if statement is calling ToBoolean, which considers any non-zero, non-NaN number to be true.

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

Comments

3

Any JavaScript developer really needs to look at the documentation -- for this case, located here: http://www.ecma-international.org/ecma-262/5.1/#sec-9.2

9.2 ToBoolean

The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:

  • Argument Type Result
  • Undefined false
  • Null false
  • Boolean The result equals the input argument (no conversion).
  • Number The result is false if the argument is +0, −0, or NaN; otherwise the result is true.
  • String The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
  • Object true

(Sorry about the formatting, can't make a table here.)

3 Comments

Is this what is used behind the scenes, so to speak, when JavaScript converts an if statement's expression to a boolean? I'd imagine !!{expression} would be ever-so-slightly faster.
@DesertIvy ! covertly calls ToBoolean: ecma-international.org/ecma-262/5.1/#sec-11.4.9
@DesertIvy Yes, this is what happens behind the scenes. And no, !! wouldn't be any faster because this ToBoolean would still have to be called on the first !, then again on the second.
1

From JavaScript The Definitive Guide

The following values convert to, and therefore work like, false:

  • undefined
  • null
  • 0
  • -0
  • NaN
  • "" // the empty string

All other values, including all objects (and arrays) convert to, and work like, true. false, and the six values that convert to it, are sometimes called falsy values, and all other values are called truthy.

Comments

1

These things by themselves are falsy (or evaluate to false):

  • undefined
  • null
  • 0
  • '' or ""
  • false
  • NaN

Everything else i truthy.

Truthy-ness or falsy-ness is used when evaluating a condition where the outcome is expected to be either truthy (true) or falsy (false).

In your example if(-1 == true), you are comparing apples and oranges. The compare is evaluated first (and resulted in false), and the results of that is used in your condition. The concept of truthyness/falsyness isn't applied to the operands the comparison.

Comments

0

When if state using with comparing variable different type js use .toString и .valueOf ( for more information check http://javascript.info/tutorial/object-conversion ) - just keep this in mind - it make so example much more easy to understand

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.