最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.jQuery==true evaluates to false, although window.jQuery!=false evaluates to true - Stack Overflow

programmeradmin3浏览0评论

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?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Sep 2, 2013 at 21:30 Robbie JWRobbie JW 7391 gold badge9 silver badges22 bronze badges 4
  • 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 and true) to pare equal to each other. That's another thing entirely. – Jon Commented Sep 2, 2013 at 21:37
  • 1 5 is truthy, and 10 is truthy, but you wouldn't expect 5 == 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
Add a ment  | 

4 Answers 4

Reset to default 10

jQuery 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:

  1. window.jQuery != false
  2. window.jQuery.toString() != 0
  3. "function (e,t){return new x.fn.init(e,t,r)}" != 0
  4. Number("function (e,t){return new x.fn.init(e,t,r)}") != 0
  5. NaN != 0
  6. true

For window.jQuery == true, something similar happens:

  1. window.jQuery == true
  2. window.jQuery.toString() == 1
  3. "function (e,t){return new x.fn.init(e,t,r)}" == 1
  4. Number("function (e,t){return new x.fn.init(e,t,r)}") == 1
  5. NaN == 1
  6. 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论