You can use a forโ€ฆin loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:

function isEmpty(obj) {
  for (const prop in obj) {
    if (Object.hasOwn(obj, prop)) {
      return false;
    }
  }

  return true;
}

If you also need to distinguish {}-like empty objects from other objects with no own properties (e.g. Dates), you can do various (and unfortunately need-specific) type checks:

function isEmptyObject(value) {
  if (value == null) {
    // null or undefined
    return false;
  }

  if (typeof value !== 'object') {
    // boolean, number, string, function, etc.
    return false;
  }

  const proto = Object.getPrototypeOf(value);

  // consider `Object.create(null)`, commonly used as a safe map
  // before `Map` support, an empty object as well as `{}`
  if (proto !== null && proto !== Object.prototype) {
    return false;
  }

  return isEmpty(value);
}

Note that comparing against Object.prototype like in this example will fail to recognize cross-realm objects.

Do not use Object.keys(obj).length. It is O(N) complexity because it creates an array containing all the property names only to get the length of that array. Iterating over the object accomplishes the same goal but is O(1).

For compatibility with JavaScript engines that donโ€™t support ES 2022+, const can be replaced with var and Object.hasOwn with Object.prototype.hasOwnProperty.call:

function isEmpty(obj) {
  for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
      return false;
    }
  }

  return true
}

Many popular libraries also provide functions to check for empty objects:

jQuery:

jQuery.isEmptyObject({}); // true

lodash:

_.isEmpty({}); // true

Underscore:

_.isEmpty({}); // true

Hoek:

Hoek.deepEqual({}, {}); // true

ExtJS:

Ext.Object.isEmpty({}); // true

AngularJS (version 1):

angular.equals({}, {}); // true

Ramda:

R.isEmpty({}); // true
Top answer
1 of 16
7554

You can use a forโ€ฆin loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:

function isEmpty(obj) {
  for (const prop in obj) {
    if (Object.hasOwn(obj, prop)) {
      return false;
    }
  }

  return true;
}

If you also need to distinguish {}-like empty objects from other objects with no own properties (e.g. Dates), you can do various (and unfortunately need-specific) type checks:

function isEmptyObject(value) {
  if (value == null) {
    // null or undefined
    return false;
  }

  if (typeof value !== 'object') {
    // boolean, number, string, function, etc.
    return false;
  }

  const proto = Object.getPrototypeOf(value);

  // consider `Object.create(null)`, commonly used as a safe map
  // before `Map` support, an empty object as well as `{}`
  if (proto !== null && proto !== Object.prototype) {
    return false;
  }

  return isEmpty(value);
}

Note that comparing against Object.prototype like in this example will fail to recognize cross-realm objects.

Do not use Object.keys(obj).length. It is O(N) complexity because it creates an array containing all the property names only to get the length of that array. Iterating over the object accomplishes the same goal but is O(1).

For compatibility with JavaScript engines that donโ€™t support ES 2022+, const can be replaced with var and Object.hasOwn with Object.prototype.hasOwnProperty.call:

function isEmpty(obj) {
  for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
      return false;
    }
  }

  return true
}

Many popular libraries also provide functions to check for empty objects:

jQuery:

jQuery.isEmptyObject({}); // true

lodash:

_.isEmpty({}); // true

Underscore:

_.isEmpty({}); // true

Hoek:

Hoek.deepEqual({}, {}); // true

ExtJS:

Ext.Object.isEmpty({}); // true

AngularJS (version 1):

angular.equals({}, {}); // true

Ramda:

R.isEmpty({}); // true
2 of 16
1500

If ECMAScript 5 support is available, you can use Object.keys():

function isEmpty(obj) {
    return Object.keys(obj).length === 0;
}

For ES3 and older, there's no easy way to do this. You'll have to loop over the properties explicitly:

function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}
Top answer
1 of 16
5789

You can just check if the variable has a truthy value or not. That means

if (value) {
    // do something..
}

will evaluate to true if value is not:

  • null
  • undefined
  • NaN
  • empty string ("")
  • 0
  • false

The above list represents all possible falsy values in ECMA-/Javascript. Find it in the specification at the ToBoolean section.

Furthermore, if you do not know whether a variable exists (that means, if it was declared) you should check with the typeof operator. For instance

if (typeof foo !== 'undefined') {
    // foo could get resolved and it's defined
}

If you can be sure that a variable is declared at least, you should directly check if it has a truthy value like shown above.

2 of 16
457

This question has two interpretations:

Check if the variable has a value
Check if the variable has a truthy value

The following answers both.

In JavaScript, a value could be nullish or not nullish, and a value could be falsy or truthy.
Nullish values are a proper subset of falsy values:

 โ•ญโ”€ nullish โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ•ญโ”€ not nullish โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”
โ”‚ undefined โ”‚ null โ”‚ false โ”‚ 0 โ”‚ "" โ”‚ ... โ”‚ true โ”‚ 1 โ”‚ "hello" โ”‚ ... โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”˜
 โ•ฐโ”€ falsy โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ โ•ฐโ”€ truthy โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Check if value is nullish (undefined or null)

Use one of the following depending on your coding style:

if (value == null)                         { /* value is nullish */ }
if (value === undefined || value === null) { /* value is nullish */ }
if (value == undefined)                    { /* value is nullish */ }
if ((value ?? null) === null)              { /* value is nullish */ }

Notes:

  • The == operator works because it has a special case for null vs undefined comparison
  • The === operator is more readable (opinion based), eqeqeq friendly and allows checking for undefined and null separately
  • The first and third examples work identically, however the third one is rarely seen in production code
  • The fourth example uses nullish coalescing operator to change nullish values to null for straight forward comparison

Check if value is not nullish

if (value != null)                         { /* value is not nullish, although it could be falsy */ }
if (value !== undefined && value !== null) { /* value is not nullish, although it could be falsy */ }
if (value != undefined)                    { /* value is not nullish, although it could be falsy */ }
if ((value ?? null) !== null)              { /* value is not nullish, although it could be falsy */ }

Check if value is falsy

Use the ! operator:

if (!value) { /* value is falsy */ }

Check if value is truthy

if (value) { /* value is truthy */ }

Data validation

The nullish, falsy and truthy checks cannot be used for data validation on their own. For example, 0 (falsy) is valid age of a person and -1 (truthy) is not. Additional logic needs to be added on case-by-case basis. Some examples:

/*
 * check if value is greater than/equal to 0
 * note that we cannot use truthy check here because 0 must be allowed
 */
[null, -1, 0, 1].forEach(num => {
  if (num != null && num >= 0) {
    console.log("%o is not nullish and greater than/equal to 0", num);
  } else {
    console.log("%o is bad", num);
  }
});

/*
 * check if value is not empty-or-whitespace string
 */
[null, "", " ", "hello"].forEach(str => {
  if (str && /\S/.test(str)) {
    console.log("%o is truthy and has non-whitespace characters", str);
  } else {
    console.log("%o is bad", str);
  }
});

/*
 * check if value is not an empty array
 * check for truthy before checking the length property
 */
[null, [], [1]].forEach(arr => {
  if (arr && arr.length) {
    console.log("%o is truthy and has one or more items", arr);
  } else {
    console.log("%o is bad", arr);
  }
});

/*
 * check if value is not an empty array
 * using optional chaining operator to make sure that the value is not nullish
 */
[null, [], [1]].forEach(arr => {
  if (arr?.length) {
    console.log("%o is not nullish and has one or more items", arr);
  } else {
    console.log("%o is bad", arr);
  }
});
๐ŸŒ
SamanthaMing
samanthaming.com โ€บ tidbits โ€บ 94-how-to-check-if-object-is-empty
How to Check if Object is Empty in JavaScript | SamanthaMing.com
isObjectEmpty(null); // false isObjectEmpty(undefined); // false ยท There are tons of external libraries you can use to check for empty objects.
๐ŸŒ
DEV Community
dev.to โ€บ onlinemsr โ€บ 7-easy-ways-to-check-if-an-object-is-empty-in-javascript-ddm
7 Easy Ways To Check If An Object Is Empty In JavaScript - DEV Community
July 5, 2023 - When the object is not null or undefined, we check if the object is empty using the Object.keys() method. If the object is empty, we print a message to the console โ€œThe object is empty.โ€ ยท If the JavaScript object is not empty, we print ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ check-if-an-object-is-empty-in-javascript
How to Check if an Object is Empty in JavaScript โ€“ JS Java isEmpty Equivalent
November 7, 2024 - Note: Checking the length alone is not the best option when checking if an object is empty or for any datatype. It is always best to confirm if the data type is correct. ... const isObjectEmpty = (objectName) => { return Object.keys(objectName).length === 0 && objectName.constructor === Object; } This way, you are liable to get a more thorough check. Thus far, everything has worked fine. But you might also want to avoid throwing a TypeError when a variable is undefined or a value of null is passed instead of {}. To fix this, you can add an extra check:
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i test for an empty javascript object?
How do I Test for an Empty JavaScript Object? | Sentry
If you donโ€™t know if the value is an object, youโ€™ll need to add some extra checks to determine whether it is. First, check if the value is null or undefined:
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ how to check for null, undefined, or empty values in javascript
How to check for null, undefined, or empty values in JavaScript - LogRocket Blog
February 14, 2025 - Learn how to write a null check function in JavaScript and explore the differences between the null and undefined attributes.
๐ŸŒ
Bonsaiilabs
bonsaiilabs.com โ€บ check-empty-object
How to check if a JavaScript Object is Empty - bonsaiilabs
Our goal is to check if users contain any key value pairs or to say entries inside it. To check for empty objects, JavaScript provides a method on objects called entries. It returns an array of entries an object contains.
Find elsewhere
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-test-for-an-empty-object-in-javascript.php
How to Test For an Empty Object in JavaScript
Topic: JavaScript / jQueryPrev|Next ยท You can simply use the Object.keys() method along with the typeof operator to test for an empty JavaScript object (contains no enumerable properties).
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-check-if-object-is-empty
How to Check If an Object Is Empty in JavaScript | Built In
July 22, 2024 - If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty.
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ i often find myself writing object.keys(someobject) > 0 to test if an object isn't {} (empty) there must be a more beautiful way.
r/javascript on Reddit: I often find myself writing Object.keys(someObject) > 0 to test if an object isn't {} (empty) there must be a more beautiful way.
September 4, 2018 -

Hi everyone,

I very often find myself writing something like

if( Object.keys(someObject).length > 0 ) {

//do some wild stuff

}

To check if a basic object is empty or not i.e. not {} there must be a beautiful way.

I know lodash and jQuery have their solutions, but I don't want to import libraries for a single method, so I'm about to write a function to use across a whole project, but before I do that I want to know I'm not doing something really stupid that ES6/ES7/ES8 can do that I'm just not aware of.

edit solution courtesy of u/vestedfox

Import https://www.npmjs.com/package/lodash.isequal

Total weight added to project after compilation: 355 bytes

Steps to achieve this.

npm i --save lodash.isequal

then somewhere in your code

const isEqual = require('lodash.isequal');

If you're using VueJS and don't want to have to include this in every component and don't want to pollute your global namespace you can do this in app.js

const isEqual = require('lodash.isequal');
Vue.mixin({
  methods: {
    isEqual: isEqual
  }
});

Then in your components you can simply write.

if( this.isEqual(someObject, {})) {
   console.log('This object has properties');
}

๐ŸŒ
Zipy
zipy.ai โ€บ blog โ€บ how-do-i-test-for-an-empty-javascript-object
how do i test for an empty javascript object
April 12, 2024 - Throughout this article, we'll explore various techniques for checking if an object is empty in JavaScript. We'll cover built-in methods, custom functions, and best practices to ensure efficient and effective object handling.
๐ŸŒ
Medium
medium.com โ€บ @tempmailwithpassword โ€บ javascript-checking-for-null-empty-or-undefined-variables-9da87dfc9c8c
JavaScript Checking for Null, Empty, or Undefined Variables
August 24, 2024 - JavaScript does not have a built-in method specifically for checking if an object is empty, but you can use Object.keys(obj).length === 0 to determine if an object has no own properties.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i check for an empty/undefined/null string in javascript?
How do I Check for an Empty/Undefined/Null String in JavaScript? | Sentry
function isEmpty(value) { return ... console.log(isEmpty(undefined)); // true ยท The isEmpty function uses the equality operator (==) to check if the argument value is null or undefined....
๐ŸŒ
Medium
medium.com โ€บ @onlinemsr โ€บ javascript-object-empty-9f811ce54587
7 Easy Ways To Check If An Object Is Empty In JavaScript | by Raja MSR | Medium
July 5, 2023 - Learn in JavaScript check if object is empty with Object.keys(), forโ€ฆin loop, Object.values(), Object.entries() & more.
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ check-if-object-is-empty-javascript
How to Check if an Object is Empty in JavaScript - Scaler Topics
December 8, 2022 - We can use Object.entries() similar to how we used methods Object.getOwnPropertNames() and Object.values() to evaluate whether an object is empty. Take a look at the example mentioned below.
๐ŸŒ
Mastering JS
masteringjs.io โ€บ tutorials โ€บ fundamentals โ€บ empty-object-truthy
Empty Objects are Truthy in JavaScript? - Mastering JS
To work around this, you should check beforehand if the argument being passed is null. const value = null; if (typeof value === 'object' && value != null && Object.keys(value).length == 0) { console.log('I will not print and not throw an error either'); }
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ react-check-if-object-is-empty-a-simple-guide-for-developers
React Check if Object is Empty: A Simple Guide
November 6, 2024 - Object.keys() offers a straightforward way to check if an object is empty in JavaScript, making it particularly useful in React applications.