Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does if("string") evaluate "string" as true but if ("string"==true) does not?

Tags:

javascript

Given the following code:

if ("string") {     console.log('true!'); } //logs "true" to the console if ("string"==true) {     console.log('true!'); } //doesn't log anything 

Why does this happen? I thought "string" was being cast to a number, as is the boolean. So true becomes 1, and "string" becomes NaN. The second if statement makes sense, but I don't see why the first statement causes the inner loop to be evaluated. What's going on here?

like image 651
munchybunch Avatar asked Feb 07 '11 16:02

munchybunch


People also ask

Does a string evaluate to TRUE?

Any non-empty string evaluates to true.

Why True == true is false in Javascript?

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible.

Is string true Python?

Any string is True , except empty strings. Any number is True , except 0 . Any list, tuple, set, and dictionary are True , except empty ones.

Is equal to true Javascript?

Use the strict equality (===) operator to check if a variable is equal to true - myVar === true . The strict equality operator will return true if the variable is equal to true , otherwise it will return false . Copied!


1 Answers

It is being cast to Boolean. Any non-empty string evaluates to true.

From the ECMAScript Language Specification:

12.5 The if statement

Semantics

The production IfStatement: if ( Expression ) Statement else Statement is evaluated as follows:

  1. Let exprRef be the result of evaluating Expression.
  2. If ToBoolean(GetValue(exprRef)) is true, then
    • Return the result of evaluating the first Statement.
  3. Else,
    • Return the result of evaluating the second Statement.

9.2 ToBoolean

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

Table 11 - ToBoolean Conversions

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


As far as the == operator is concerned, it's complicated, but the gist of it is that if you compare a number to a non-number the latter is converted into a number. If you compare a boolean against a non-boolean, the boolean is first converted to a number, and then the previous sentence applies.

See section 11.9.3 for details.

// Call this x == y. if ("string" == true)   // Rule 6: If Type(y) is Boolean, //         return the result of the comparison x == ToNumber(y). if ("string" == Number(true))  // Rule 5: If Type(x) is String and Type(y) is Number, //         return the result of the comparison ToNumber(x) == y.   if (Number("string") == Number(true))  // The above is equivalent to: if (NaN == 1)  // And NaN compared to *anything* is false, so the end result is: if (false) 
like image 150
John Kugelman Avatar answered Oct 14 '22 11:10

John Kugelman