5

How does javascript if condition determines its value?, see this example:

<script type="text/javascript">

var bar = ("something" == true);
alert(bar); // 1

if ("something") {
    alert("hey!"); // 2
}

</script>

Why do I get to point //2 while 'bar' at //1 is false?

As I can see bar value gets calculated in almost the same way the if condition, or it doesn't?

Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57

5 Answers5

4

"something" == true is false because the string and the boolean have to be coerced into types that can be compared. However, if("something") works because a non-empty string is a truthy value.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
2

It's because of how the javascript type coercion engine works. When you say

"something" == true

javascript calls ToNumber on your "something" string to compare it to the boolean. "something" produce NaN which does not equal true.

However

if("something")

only checks if the string is truthy. Because it's not an empty string, it is in fact truthy.

More here: http://webreflection.blogspot.co.il/2010/10/javascript-coercion-demystified.html

just.another.programmer
  • 8,579
  • 8
  • 51
  • 90
1

That's because in the first case, JavaScript will attempt to compare them as strings (lit. "something" == "true"), which will be false.

However, in the second condition, the result of the expression is "something", which when cast to a Boolean, is true.

See here for details.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • But `"true" == true` evaluates to `false` too. If its just comparing as Strings(like you have indicated), this should have been `true`. – Sujay Jul 18 '12 at 17:15
1
if("something")

The declaration above will return true because "something" is a valid string. It would return false if it was an empty string (""). It also happens with number (0 returns false, but 1 returns true).

In "something"==true, both are converted to strings and then checked ("something"=="true", which will return false).

Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
0

The thing is not so much how javascript handles if statements as it is how javascript coerces object types.

A non empty string is truthy, although it does not equal true. You can check this if you trye "something != false which returns true.

Because of this a lot of people advocate the strict comparison in JavaScript to avoid these pitfalls.

For example:

"something" !== false // true
"something" === true  // false
"" === false          // false
0 === false           // false

To read up on this, there are a ton of articles. I'd recommend Douglas Crockford.

Torsten Walter
  • 5,614
  • 23
  • 26
  • Now I'm confused, both `"something" == false` and `"something" == true` are false, why? – Jaime Hablutzel Jul 18 '12 at 17:22
  • Because when `==` tries to do its type coercion thing, `true`, `false`, and `'something'` always come out to be the different values. Not really surprising in that case, considering `'something'` isn't a boolean anyway (so comparing them doesn't make sense). – cHao Jul 18 '12 at 17:26
  • Read again, it says `("something" != false)` and `("something" == false)` – Torsten Walter Jul 18 '12 at 18:04