suman suman - 26 days ago 5
AngularJS Question

Javascript If condition to check a variable with empty object or not

I have a variable var a = {};

if ( a !==null) {
// Entering here if var a has empty object but i don't want to enter.
}

if (a == null) {
// Want to enter into this condition if var a has empty object.
}


I have tried several ways to write this condition like giving a=={} but it still entering first condition. Could you please let me know the appropriate way to check that condition?

Answer

First option

var a = null;

Second option

if ( a !==null && JSON.stringify(a) !== '{}') {..}