JavaScript 'typeof' summary
Blogs20122012-06-29
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' )
-> trueSo 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.
