Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Check If an Object is Empty in JavaScript</title> <script> // Defining a function function isEmptyObject(obj) { if(typeof obj === 'object' && obj != null && Object.keys(obj).length !== 0){ return false; } else { return true; } } // Testing few values document.write(isEmptyObject({}) + "<br>"); // Prints: true document.write(isEmptyObject({name: "Harry", age: 18}) + "<br>"); // Prints: false document.write(isEmptyObject(null) + "<br>"); // Prints: true document.write(isEmptyObject(undefined)); // Prints: true </script> </head> <body style="font-family: monospace;"> <!-- Result will be printed here --> </body> </html>