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 200 views
2 answers 169 views
1 answer 137 views
2 answers 230 views
2 answers 243 views
1 answer 171 views
...