How To Check if an Object is Empty in JavaScript Tutorial

Reading Time: 4 minutes
829 Views

Inside this article we will see the concept i.e How To Check a Prime Number in PHP Tutorial. Article contains the classified information about a program to How to check an object is empty using JavaScript.

If you are looking for a solution i.e How to Check if an Object is Empty in JavaScript then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Learn More –

Let’s get started.

Example #1: Javascript Program To Check Object Empty

Here,

We will use the concept of Javascript i.e How to Check if an Object Is Empty in JavaScript.

To check if an object is empty in JavaScript we need to pass the object to the Object.keys method to get an array of the object’s keys then we access the length property on the array.

const obj = {};

const isEmpty = Object.keys(obj).length === 0;

console.log(isEmpty); // true

Console Output

Example #2: Javascript Program To Check Object

Here,

We will use the concept of Javascript i.e How to Check if an Object Is Empty in JavaScript.

const obj = {first: 1, second: 2};

console.log(Object.keys(obj)); // ['first', 'second']

const isEmpty = Object.keys(obj).length === 0;

console.log(isEmpty); // false

Console Output

Example #3: Javascript Program To Check Object Using ‘for’ Loop

Here,

We will use the concept of Javascript i.e How to Check if an Object Is Empty in JavaScript.

const obj = {};

function isEmpty(object) {
  for (const property in object) {
    return false;
  }
  return true;
}

console.log(isEmpty(obj)); // true

Console Output

We hope this article helped you to learn How To Check if an Object is Empty in JavaScript Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more