Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Determine If a Value is an Object in JavaScript</title> <script> // Defining a function function isObject(val) { if(typeof val === 'object' && val !== null && Array.isArray(val) === false){ return true; } else { return false; } } // Testing few values document.write(isObject({}) + "<br>"); // Prints: true document.write(isObject({name: "Alice", age: 24}) + "<br>"); // Prints: true document.write(isObject(new Date()) + "<br>"); // Prints: true document.write(isObject([1, 2, 3]) + "<br>"); // Prints: false document.write(isObject(null) + "<br>"); // Prints: false document.write(isObject("John") + "<br>"); // Prints: false document.write(isObject(function(){}) + "<br>"); // Prints: false document.write(isObject(8)); // Prints: false </script> </head> <body> <!-- Result will be printed here --> </body> </html>