suman suman - 1 year ago 74
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 Source

First option

var a = null;

Second option

if ( a !==null && JSON.stringify(a) !== '{}') {..}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download