Navigate back to the homepage

How to check if an object is empty in javascript

Infinitbility
JavaScript
August 30th, 2021 · 1 min read
How to check if an object is empty in javascript

Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to verify your object empty or not in javascript, here we will see examples of check empty object in Ecma 5, Pre-ECMA 5, jQuery, lodash, Underscore, Hoek, ExtJS, AngularJS (version 1), and Ramda.

Let’s take example of empty object.

1const emptyObject = {};

Before going to check empty object, if want verify data is a object or not then you option to use javascript typeof keyword.

1if(typeof emptyObject === "object"){
2 // code goes here
3}

JavaScript

JavaScript provide Object.entries() function to return an array containing the object’s enumerable properties.

Due to javascript codebase it will work also on React, React Native, typescript, node, and deno.

use like below:

1Object.entries(emptyObject)

it returns an empty array, it means the object does not have any enumerable property, which in turn means it is empty.

  • Object.entries() if example
1if(Object.entries(emptyObject).length === 0){
2 // code goes here
3}

ECMA 5+

Below empty object check example for ECMA 5+ developers.

1if(Object.keys(obj).length === 0 && obj.constructor === Object){
2 // code goes here
3}

Pre-ECMA 5

Well if you are use ECMA previous versions.

1function isEmpty(obj) {
2 for(var prop in obj) {
3 if(obj.hasOwnProperty(prop)) {
4 return false;
5 }
6 }
7
8 return JSON.stringify(obj) === JSON.stringify({});
9}

jQuery

jQuery provide isEmptyObject() function to verify object empty or not, use like below example.

1jQuery.isEmptyObject(emptyObject); // true
2jQuery.isEmptyObject({ foo: "bar" }); // false

lodash

lodash provide isEmpty() function to verify object empty or not, use like below example.

1_.isEmpty(emptyObject); // true

Underscore

Same like lodash, Underscore provide isEmpty() function to verify object empty or not, use like below example.

1_.isEmpty({}); // true

Hoek

Hoek provide deepEqual() function to verify object data but you will also use for check empty object.

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

ExtJS

ExtJS provide Object prototype isEmpty() function to verify object empty or not, use like below example.

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

AngularJS

Same like Hoek, AngularJS provide equals() function to verify object data but you will also use for check empty object.

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

Ramda

Ramda provide isEmpty() function to verify object empty or not, use like below example.

1R.isEmpty({}); // true

Thanks for reading…

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Tutorials

React Native

Categories

More articles from Infinitbility

How to get file details in react native

How to get file details in react native

React Native FS stat example to get file name, file type, file created date, file last modified date, and file size.

August 29th, 2021 · 1 min read
How to get seconds between two dates in javascript

How to get seconds between two dates in javascript

Get seconds differance between two dates in javascript example

August 28th, 2021 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]