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?

4

5 Answers 5

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.

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

Comments

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

Comments

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.

1 Comment

But "true" == true evaluates to false too. If its just comparing as Strings(like you have indicated), this should have been true.
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).

Comments

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.

3 Comments

Now I'm confused, both "something" == false and "something" == true are false, why?
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).
Read again, it says ("something" != false) and ("something" == false)

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.