Created
October 2, 2025 09:57
-
-
Save detj/719e9035601417de0a5b830b06e7f0b6 to your computer and use it in GitHub Desktop.
isObject(something)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Check if 'something' is just a plain JavaScript object | |
| // like a map of keys and values. | |
| function isObject(something) { | |
| const type = Object.prototype.toString.call(something); | |
| return something === Object(something) && type !== "[object Array]" && type !== "[object Function]" && type !== "[object Date]" && type !== "[object RegExp]" && type !== "[object Set]" && type !== "[object Map]"; | |
| } | |
| // truthy | |
| console.log("{}", isObject({})); | |
| console.log("Object.create(null)", isObject(Object.create(null))) | |
| // falsy | |
| console.log("function() {}", isObject(function() {})); | |
| console.log("new Date()", isObject(new Date())); | |
| console.log("[]", isObject([])); | |
| console.log("new RegExp()", isObject(new RegExp())); | |
| console.log("null", isObject(null)); | |
| console.log("undefined", isObject(undefined)); | |
| console.log("some text", isObject("some text")); | |
| console.log("true", isObject(true)); | |
| console.log("5", isObject(5)); | |
| console.log("1n", isObject(1n)); | |
| console.log("Symbol(\"foo\")", isObject(Symbol("foo"))); | |
| console.log("new Set()", isObject(new Set())); | |
| console.log("new Map()", isObject(new Map())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment