How does JavaScript evaluate if statement expressions? - Stack Overflow

I always thought that JavaScript's if statements did some kind of casting magic to their arguments

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 parison 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?

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 parison 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?

Share Improve this question edited Mar 28, 2014 at 21:09 Bucket asked Mar 28, 2014 at 20:56 BucketBucket 7,5319 gold badges37 silver badges48 bronze badges 4
  • I don't understand -- -1 is truthy, so it will execute. (truthy != true) – Sterling Archer Commented Mar 28, 2014 at 20:58
  • 3 there is a difference between something being "truthy" and something being (boolean) true value – CrayonViolent 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 – CrayonViolent Commented Mar 28, 2014 at 21:01
  • ecma-international/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 parison x == ToNumber(y). – Yury Tarabanko Commented Mar 28, 2014 at 21:05
Add a ment  | 

5 Answers 5

Reset to default 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 parison x == ToNumber(y).

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

-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.

Any JavaScript developer really needs to look at the documentation -- for this case, located here: http://www.ecma-international/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.)

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.

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 oute is expected to be either truthy (true) or falsy (false).

In your example if(-1 == true), you are paring apples and oranges. The pare 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 parison.

When if state using with paring 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744307265a4567776.html

相关推荐

  • How does JavaScript evaluate if statement expressions? - Stack Overflow

    I always thought that JavaScript's if statements did some kind of casting magic to their arguments

    7天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信