0

I'm creating one Object and assign null value then push some values into created object now i want check object is null or not?

here is my code

var data = {};
    if(typeof first_name != 'undefined'){
        data['first_name'] = first_name;
    }

    if(typeof last_name != 'undefined'){
        data['last_name'] = last_name;
    }

//checking value is null or not

if(typeof data !== null){
    console.log(data);
    }
    else{
    console.log('No Any values');   
    }

its give O/P this: Object {}

Kaushik Makwana
  • 2,422
  • 9
  • 31
  • 50

1 Answers1

1
if(Object.keys(data).length==0){
  // No key value pair present
}

Or in jQuery you can do

if(jQuery.isEmptyObject(data)){
   // No key value pair present
} 
void
  • 36,090
  • 8
  • 62
  • 107