Skip to content

Instantly share code, notes, and snippets.

@ryanmorr
Last active March 29, 2024 06:40
Show Gist options
  • Save ryanmorr/3da83b1fb4ac4c454da58548c21245f4 to your computer and use it in GitHub Desktop.
Save ryanmorr/3da83b1fb4ac4c454da58548c21245f4 to your computer and use it in GitHub Desktop.
Check if an object is an object literal or bare object
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