This is something I cannot get my head round.
It was my understanding that JavaScript had truthy and falsy values:
Falsy values:
0
false
undefined
null
NaN
"" (empty string)
Truthy values:
Anything that isn't a falsy value
If window.jQuery has loaded correctly, then it shouldn't evaluate to false (or rather, undefined
). The following condition will return true
:
window.jQuery != false
However, the following condition will return false
:
window.jQuery == true
(I'm using ==
for all of these, rather than ===
, otherwise window.jQuery will always evaluate to false
unless it is literally a boolean containing the value false
).
What is happening here? Surely if a condition doesn't evaluate to false
, then it must evaluate to true
?
This is something I cannot get my head round.
It was my understanding that JavaScript had truthy and falsy values:
Falsy values:
0
false
undefined
null
NaN
"" (empty string)
Truthy values:
Anything that isn't a falsy value
If window.jQuery has loaded correctly, then it shouldn't evaluate to false (or rather, undefined
). The following condition will return true
:
window.jQuery != false
However, the following condition will return false
:
window.jQuery == true
(I'm using ==
for all of these, rather than ===
, otherwise window.jQuery will always evaluate to false
unless it is literally a boolean containing the value false
).
What is happening here? Surely if a condition doesn't evaluate to false
, then it must evaluate to true
?
- Because you're ultimately paring a string to a number. See ecma-international/ecma-262/5.1/#sec-11.9.3 – bfavaretto Commented Sep 2, 2013 at 21:32
-
1
JavaScript does have truthies and falsies, but what you are expecting here is two different truthies (
jQuery
andtrue
) to pare equal to each other. That's another thing entirely. – Jon Commented Sep 2, 2013 at 21:37 -
1
5
is truthy, and10
is truthy, but you wouldn't expect5 == 10
to be true, would you? – Barmar Commented Sep 2, 2013 at 21:38 - !(window.jQuery == true) is not the same thing as window.jQuery != false. These are two different expressions. – Yogesh Commented Sep 2, 2013 at 21:43
4 Answers
Reset to default 10jQuery
is a function, and it's not == true
, so you get false
, and it's != false
so you get true
.
I think you've got the idea that a ==
parison is the same as a boolean conversion. It's not. To do a boolean conversion, you could do Boolean(window.jQuery) == true
, and you'll get true
. Or just !!window.jQuery == true
.
When you convert to a boolean value, then you get conversion to true
in all cases except false
, null
, undefined
, NaN
, ""
and 0
.
Ultimately if you want to see if jQuery is loaded, then you'd just do...
if (window.jQuery) {
Which will perform the boolean conversion for you.
When paring values of different types, some internal conversions happen implicitly. Assuming jQuery is being used, you would have the following:
For window.jQuery != false:
- window.jQuery != false
- window.jQuery.toString() != 0
- "function (e,t){return new x.fn.init(e,t,r)}" != 0
- Number("function (e,t){return new x.fn.init(e,t,r)}") != 0
- NaN != 0
- true
For window.jQuery == true, something similar happens:
- window.jQuery == true
- window.jQuery.toString() == 1
- "function (e,t){return new x.fn.init(e,t,r)}" == 1
- Number("function (e,t){return new x.fn.init(e,t,r)}") == 1
- NaN == 1
- false
Source: http://javascriptweblog.wordpress./2011/02/07/truth-equality-and-javascript/
When you pare values with true
or false
, it doesn't just pare their truthiness. true
and false
are specific values, just like different numbers and strings. Just because two values have the same truthiness, doesn't mean they actually pare equal.
If you want to pare the truthiness of two values, you can do it by forcing them to boolean types. A simple way to do this is with boolean inversion:
if (!foo == !bar)
will tell if foo
and bar
have the same truthiness.
If you're trying to see if jQuery is loaded or not. This will tell you.
if (jQuery) {
// jQuery is loaded
} else {
// jQuery is not loaded
}
Check out this blog post:
Check if jQuery is Loaded