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?
It’s because of how the javascript type coercion engine works. When you say
javascript calls ToNumber on your “something” string to compare it to the boolean. “something” produce NaN which does not equal true.
However
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