Back to blog

How to check if an Object has values? 💻

When working with multiple objects, it can be difficult to keep track of which ones have actual values and which ones can be discarded. But did you know there's a simple hack to check if an object is empty or not?

The trick is to use the Object.keys() function, which returns an array of an object's keys. If the length of the returned array is zero, it means the object doesn't have any assigned values.

const obj = { a: 1, b: 2, c: 3 };const obj2 = {};console.log(Object.keys(obj).length); // 3console.log(Object.keys(obj2).length); // 0

This way, you can easily check if an object has any values or not, making it easier to manipulate objects in your code.

So, had you heard of this trick before? Share your experience with me in the Instagram! If you have any questions, don't hesitate to ask.

Let's do this! 🦅


Previous post Next post

Comments (0)

This article has no comments yet 😢. Be the first! 🚀🦅

Add comments