How To Check if an Object is Empty in JavaScript Tutorial

Share this Article
Reading Time: 4 minutes
350 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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5