William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

JavaScript ‘typeof’ summary

JavaScript ‘typeof’ summary

Here I list some special characters or variables, what are their types in JavaScript?
These can be used for quick retrieving.

typeof null -> "object"
typeof undefined -> "undefined", 
-- so a=null, b=undefined; -> a==b, but a!==b, if(!a) true, if(!b) true; 

typeof {} -> "object"
typeof [] -> "object"
-- so if({}), if([]) all true; if({}.length), if([].length) all false

typeof "0" -> "String"
typeof 0  -> "number"
typeof ''  -> "string", Empty String: boolean false
-- so if(0), if('') all false

typeof false -> "boolean"
typeof true -> "boolean"

//To check an object for null by look at it's length:
if(myObject.length==0){//means it is null.}
//in jQuery is:
if ( $('#myobject').length ){}
1.
var c;
-- if(!c) -> true
--> if(d) or if(!d)  -> error: ReferenceError: d is not defined

2. the following is different!
var a=undefined;
-- if(a==undefined) or if(a===undefined) -> true
-- if(typeof a==='undefined') or if(typeof a == 'undefined') 
     -> true

-- if(d == undefined ) or if(d === undefined ) 
     -> error: ReferenceError: d is not defined
-- if(typeof d == 'undefined' ) or if(typeof d === 'undefined' )
     -> true

So here var a=undefined and directly use d without declare d before using it:(var d; d=undefined; d=null; d==”…) is different.
For such case, always use if(typeof abcdef==’undefined’) is safe.

About these ads

One response to “JavaScript ‘typeof’ summary

  1. nandrolone 07/02/2012 at 2:30 am

    magnificent submit, very informative. I’m wondering why the opposite experts of this sector don’t notice this. You should continue your writing. I’m sure, williamjxj.wordpress.com have a huge readers’ base already!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 73 other followers

%d bloggers like this: