-3

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!

mihai
  • 37,072
  • 9
  • 60
  • 86
Treper
  • 3,539
  • 2
  • 26
  • 48
  • 2
    I guess you're looking for `typeof`: `typeof(foo)` --> "undefined" – Ashwini Chaudhary Jun 13 '13 at 08:50
  • 2
    Indeed. ```obj==='undefined'``` assumes that ```obj``` is a string with the value of ```undefined```. – DarthJDG Jun 13 '13 at 08: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 09:07
  • also in python you should not be using `type`, you should be using `isinstance` – jamylak Jun 13 '13 at 09:34

1 Answers1

1
> 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.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504