Last active
March 29, 2024 06:40
-
-
Save ryanmorr/3da83b1fb4ac4c454da58548c21245f4 to your computer and use it in GitHub Desktop.
Check if an object is an object literal or bare object
This file contains 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
function isPlainObject(value) { | |
if (!value || typeof value !== 'object') { | |
return false; | |
} | |
const prototype = Object.getPrototypeOf(value); | |
return prototype === null || prototype === Object.getPrototypeOf({}); | |
} | |
// Usage: | |
isPlainObject({}); //=> true | |
isPlainObject(new Object()); //=> true | |
isPlainObject(Object.create(null)); //=> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment