Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I want to know whether a object is null or undefined so I use the following code:

if(obj==='undefined'||obj===null)

But it doesn't seem to work. Is there similar python type command to get the type of obj in node.js shell? Thanks!

share|improve this question

marked as duplicate by kapa, jamylak, TerryA, Joe, David M Jun 13 '13 at 10:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
I guess you're looking for typeof: typeof(foo) --> "undefined" –  Ashwini Chaudhary Jun 13 '13 at 8:50
2  
Indeed. obj==='undefined' assumes that obj is a string with the value of undefined. –  DarthJDG Jun 13 '13 at 8:55
    
Underscore.js has some handy functions to check for variable types (isNull and isUndefined in your case), but nothing can substitute learning Javascript of course :). –  kapa Jun 13 '13 at 9:07
    
also in python you should not be using type, you should be using isinstance –  jamylak Jun 13 '13 at 9:34

1 Answer 1

up vote 1 down vote accepted
> typeof foo == 'undefined'
true
> typeof 1 == 'number'
true

This should work for you:

if( typeof obj === 'undefined' || obj === null)

From docs:

The typeof operator returns a string indicating the type of the unevaluated operand.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.