How to check empty objects in JavaScript18 Mar 2025 | 3 min read A collection of properties is stored in an object. In an object, there is an association of a name, also called a key and a value combined with called key and value pairs. But then, external dependency is not required by it when it refers to small applications. With pure javascript, it is the best method for checking if the Object is empty. Whether it is empty or not is a basic and frequent operation for checking an object. However, there are several methods for determining whether it's empty or not. Type 1:With the help of the Object.keys(Object) method in javascript: The keys in the Object will be returned by the Object.keys(Object) method, which will be passed by the required Object. To check the number of keys, the length property is used for the result. The Object will be empty when the length property returns 0 keys. Syntax: Example: Output: 1) It is the output of before clicking button: ![]() 2) This is the output for after clicking button: ![]() Type 2:Object loop using Object.hasOwnProperty(key): When the Object is looped over, then the function is created, and using the Object.hasOwnProperty() method, it checks whether it contains the 'key' property in it. If no keys in the loop are found, then this function would return true, which means that the Object is empty. If any key is encountered, the loop breaks, and the false is returned. Syntax: Example: Output: 1) It is the output of before clicking button: ![]() 2) This is the output for after clicking button: ![]() |
? As we all know, the variable declaration is one of the basic and essential aspects of any programming language such as C language, C++, etc. However, JavaScript has a small odd, known as Hoisting, which can turn a flawless-looking declaration into a subtle bug. Hoisting In JavaScript,...
3 min read
What is Underscore.js? Underscore.js is the library in JavaScript which has a lot of functions like map, filter, chain, etc., and these functions are applied directly on the objects or arrays. _.filter() function The _.filter() function is used to check and filter those elements which satisfy some specific...
4 min read
JavaScript provides a lot of built-in string functions to perform various operations on strings. One such useful string function is endsWith(), which allows you to check if a given string ends with a specific set of characters. In this article, we will explore the endsWith()...
3 min read
Blobs are immutable objects that represent unprocessed data. The file is a Blob derived from data from the file system. Blobs allow us to create file-like objects on the client that we may transfer to APIs, and these APIs expect URLs rather than needing the...
5 min read
JS First Class Function JavaScript is a dynamic scripting language that allows us to use it in the form of an object-oriented style or functional style. In the same way, one such feature of JavaScript is the First class function. In this section, we will discuss the...
4 min read
In JavaScript, Infinity is a special number with an intriguing property: greater than any finite number. We might be amazed at how infinite numbers operate in conditional statements and arithmetic operations. This operation works if we don't know the properties of Infinity beforehand. Let's look at...
6 min read
? PI is a mathematical constant. The lowercase Greek letter π represents the symbol PI. It is an acronym of the periphery. PI can be defined as the circumference (C) of a circle divided by its diameter (d). It means that for any size of the circle,...
2 min read
? In this article, we will see how to calculate the perimeter and area of a circle using JavaScript. The mathematical formula for calculating the perimeter and area of the circle is given below: Area of circle = π * r * r Perimeter of circle = 2...
1 min read
JavaScript provides an efficient garbage collection for memory management. JavaScript automatically supports memory management and is invisible to us. When we create new objects, functions, primitives and variables, all these programming elements take memory. How does JavaScript manage these elements and cleans them? In this section,...
8 min read
Javascript, being a broad and varied-natured programming language consists of an ample amount of features to convert an object into an array. These methods are predefined in the language itself. Let's look at the different ways of converting objects to arrays in javascript. To convert an...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India