simplesystems simplesystems - 6 months ago 26
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"


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`