How to check if an object is empty in JavaScript

2 Answers

0 votes
const obj = {};
 
const objEmpty = Object.keys(obj).length === 0;

console.log(objEmpty);
  
   
   
/*
run:
         
true
       
*/

 



answered Nov 5, 2019 by avibootz
edited Jul 10, 2022 by avibootz
0 votes
const obj = {};
 
const objEmpty = Object.entries(obj).length === 0 && obj.constructor === Object;

console.log(objEmpty);
  
   

   
/*
run:
         
true
       
*/

 



answered Nov 5, 2019 by avibootz
edited Jul 10, 2022 by avibootz

Related questions

2 answers 189 views
2 answers 156 views
1 answer 128 views
2 answers 210 views
2 answers 223 views
1 answer 156 views
...