simplesystems simplesystems - 1 year ago 105
Javascript Question

Javascript check if undefined doesnt work

I want to check if a object is defined or not..

content of the Object:


so I'll do:

if (e.model.item.state != "undefined"){
var stateID =;


Then e.model.item.state is undefined but it does enter the if clause and stops here:

var stateID =;

because of undefined..!

I tried also:

!== "undefined"
!=== "undefined"

Answer Source

In JS, you can check whether a variable is either undefined, null, false or 0 by just simply doing,

if (e.model.item.state) {
   // e.model.item.state is NOT `undefined`, `null`, `false` or `0`
else {
   // e.model.item.state is either `undefined`, `null`, `false` or `0`
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download