6

I always thought I could just check an undefined var by comparing it to undefined, but this is the error I get in the chrome console :

enter image description here

How how do i check for the object jQuery being undefined ?

EDIT :

enter image description here

if(jQuery) is giving me problems too

EDIT :

solution :

if(window.jQuery) works. typeof(jQuery) == 'undefined' works too.

Could anyone explain why ?

YD8877
  • 10,401
  • 20
  • 64
  • 92
  • [http://stackoverflow.com/questions/8531059/how-to-check-if-a-variable-or-object-is-undefined/8531076#8531076][1] [1]: http://stackoverflow.com/questions/8531059/how-to-check-if-a-variable-or-object-is-undefined/8531076#8531076 – Eric Yin Dec 16 '11 at 07:40
  • http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-javascript , http://stackoverflow.com/questions/8531059/how-to-check-if-a-variable-or-object-is-undefined –  Dec 16 '11 at 07:49

7 Answers7

9

There are several solutions:

  1. Use typeof. It is a special operator and will never result in a ReferenceError. It evaluates to "undefined" for, well, the undefined value or for a variable which does not exist in context. I'm not a fan of it, but it seems very common.

  2. Use window.jQuery. This forces a "property lookup": property lookups never fail, and return undefined if said property does not exist. I've seen it used in some frameworks. Has the downside of assuming a context (usually window).

  3. Make sure the variable is "declared": var jQuery; if (jQuery) { /* yay */ }. Doesn't seem very common, but it is entirely valid. Note that var is just an annotation and is hoisted. In the global context this will create the "jQuery" property.

  4. Catch the ReferenceError. Honestly, I have never seen this nor do I recommend it, but it would work.

Happy coding.

5

Process 1:

if (jQuery) {  
    // jQuery is loaded  
} else {
    // jQuery is not loaded
}

Process 2:

if (typeof jQuery == 'undefined') {  
    // jQuery is not loaded  
} else {
    // jQuery is loaded
}
Ariful Islam
  • 7,639
  • 7
  • 36
  • 54
  • 1
    No, this can throw a ReferenceError, as what is being shown in the post. –  Dec 16 '11 at 07:28
  • Process #2 is safe/correct here. Often times I see `===` used exclusively with `typeof`, but, looks valid. –  Dec 16 '11 at 08:02
3

From here

if(typeof jQuery == "undefined") { 
  document.write("undefined"); 
}else{ 
   document.write("Exists"); 
} 
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • Although this "cheats" with `var test=1` as then the context is not the same :) It would still be valid for this situation without it, though. –  Dec 16 '11 at 08:01
2

As far as I know you can do

if(typeof X == 'undefined')

But there are resources loader you might want to take a look at. And the answer given before me is also correct.

handy
  • 696
  • 3
  • 9
  • 22
1

You can use : if( typeof jQuery !== 'undefined')

or

Do what is recommended by mozilla

if('jQuery' in window)

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined

Ahmed El Kilani
  • 345
  • 1
  • 8
1

The variable called "jQuery" in your code has never been declared, so it will throw an error like "xxx(variable name) is not defined".

You can use the typeof operator to check is a variable is undefined or not

if (typeof(jQuery) == "undefined")
Interrobang
  • 16,984
  • 3
  • 55
  • 63
Lerous
  • 33
  • 6
-1

if(jQuery) should be enough, shouldn't it?

maialithar
  • 3,065
  • 5
  • 27
  • 44
  • No. It might throw a ReferenceError (what the above is in the post). –  Dec 16 '11 at 07:26