Check Status object

 1 check Null and Undefined And primative datatype

if(object){

// object is not

null, undefined, 0, '', false.

 but it can empty({})

}

example

let varNull = null;
let varUndefined = undefined;
let varNumber = 0;
let varString = '';
let varBoolean = false;

if(!varNull){  // means if varNull is null => into block it and view console
  console.log('varNull'); // it view
}

if(varNull){  // means if varNull not null => not into block it and view console
  console.log('varNull'); // it not view
}

 

 if(!varUndefined){
  console.log('varUndefined'); //view
}
if(!varNumber){
  console.log('varNumber'); // view
}
if(!varString){
  console.log('varString'); //view
}
if(!varBoolean){
  console.log('varBoolean'); //view
}

2 Check empty object

if(Object.keys(object).length === 0 && object.constructor == Object){

    // object not empty 

    // let object = {

    a: 111,

    ...

    }

 

3 Check All 

if(object && Object.keys(object).length === 0 && object.constructor === Object){

 // not null 

// not underfined

// not empty

referencen: https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object

0 comments :

Post a Comment

Cancel Reply

About Me

My photo
Tân An, Long An, Vietnam
Hello everyone, I love programming, I love making friends with people all over the world.