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,136
  • 7
  • 29
  • 49

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
  • 34,922
  • 8
  • 55
  • 102