Skip to content Skip to sidebar Skip to footer

Javascript : White Space String Boolean Conversion

In Javascript : !!' ' == true // => true ' ' == true // => false if (' ' == true){a = 1;} else {a = 0;} a; // => 0 if (' '){b = 1;} else {b = 0;} b; // => 1 Any id

Solution 1:

Let's step through your examples:

!!" " == true // => true

You've converted the string " " to a boolean by negating it. A string with nonzero length will evaluate to true when converted to a boolean, so negating it twice results in the boolean value of true. true == true obviously. This example does not result in the second parameter being converted to a string because the unary ! operator has higher precedence than the == operator, so by the time the == operator is evaluated, both operands are the same type (boolean).

" " == true // => false

Per Mozilla docs (relevant section shown in patrick dw's answer), both operands are converted to a number if either operand is a number or a boolean. You end up with 0 == 1, which is false.

In this case, I believe the second parameter is being coerced into a string. You're essentially doing " " == "true", which is false. This is a comparison of strings, not a comparison of string and boolean.

if ("  " == true){a = 1;} else {a = 0;}

This is the same as the previous one: one operand is a boolean, so both values are converted to a number.

if ("  "){b = 1;} else {b = 0;}

This is the same as the first one. The string is coerced into a boolean value. The string has a nonzero length, so it returns true.

Solution 2:

From section 9.3.1 in ECMAScript 5, with regard to converting strings to numbers:

A StringNumericLiteral that is empty or contains only white space is converted to +0.

And from MDC docs:

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.

So since the string is being compared to a boolean, an attempt is made to convert it to a number. And since an empty string or a string with only white space is converted to 0, then it would be considered false.

" " == false; // true" " == 0;     // true"false" == false;   // false because  "false" can't be converted to a number

So when you do:

!!" "

You're no longer relying on the == to do conversions for you. Instead you're converting it manually using your own criteria, which is to not convert it to a number, but rather to convert it to a boolean before the == takes place.

So you're doing true == true or probably actually 1 == 1.

To see the string with only white space converted into a number, use the unary + operator like this:

alert( +"  " );  // alerts 0

Solution 3:

String of length equal a value of true. String evaluation to Boolean is false since they're not the same datatype.

The double !! is a not-false operation usage.

So you're saying:

" " == false -> false == false -> true == true

Breakdown:

(!(!" ") == true)
(!(false) == true)
(true == true)
(true)

Post a Comment for "Javascript : White Space String Boolean Conversion"