By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,652 Members | 1,441 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,652 IT Pros & Developers. It's quick & easy.

Question on a typeof

dmjpro
100+
P: 2,476
Expand|Select|Wrap|Line Numbers
  1. if(typeof obj_name){}
  2. if(typeof obj_name!=undefined){}
  3.  
What is the difference between these two?
Basically undefined returns false ....if i want to check whether a object exists or not can't i do that simply using
[code=JAVASCRIPT]if(obj_ref){}[?CODE]
Could you explain ?
Aug 18 '08 #1
Share this Question
Share on Google+
2 Replies


100+
P: 210
Expand|Select|Wrap|Line Numbers
  1. if(typeof obj_name){}
typeof returns the type as a string, so the above always evaluates true
Expand|Select|Wrap|Line Numbers
  1. if(typeof obj_name!=undefined){}
  2.  
The above always evaluates true because undefined isn't a string.

Expand|Select|Wrap|Line Numbers
  1. if(typeof myObj != "undefined")
  2.  .......
Also you can test by naming the object as a child; so if myObj is global
Expand|Select|Wrap|Line Numbers
  1. if( window.myObj != undefined )
  2.  .......
  3.  
Aug 18 '08 #2

dmjpro
100+
P: 2,476
typeof returns the type as a string, so the above always evaluates true
The above always evaluates true because undefined isn't a string.

Expand|Select|Wrap|Line Numbers
  1. if(typeof myObj != "undefined")
  2.  .......
Also you can test by naming the object as a child; so if myObj is global
Expand|Select|Wrap|Line Numbers
  1. if( window.myObj != undefined )
  2.  .......
  3.  
Yeah Now I got it ....
Aug 18 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.