Featured post
- Get link
- X
- Other Apps
How to check an object is empty in javascript
As a programmer, we need to handle objects in our daily life.So there should be some cases where we have to check the given object is empty.Empty objects mean there is no keys for that object.In this blog post I am going to explain How we can easily identify an object is empty or not in javascript
let obj1 ={}
console.log(Object.keys(obj1)); // []
if(Object.keys(obj1).length === 0)
{
console.log("obj1 is an empty object")
}else{
console.log("obj1 is not an empty object")
} // obj1 is an empty object
let obj2 ={ name : "Technursery" , visitors : 1000}
console.log(Object.keys(obj2)); // [ 'name', 'visitors' ]
if(Object.keys(obj2).length === 0)
{
console.log("obj2 is an empty object")
}else{
console.log("obj2 is not an empty object")
} //obj2 is not an empty object
In the above code, I created two objects obj1 and obj2. obj1 is an object which has no key. So we got an empty array for keys and result is obj1 is an empty object .But obj2 is an object which has two keys. So we got an array with two keys as shown in the above code.So obj2 is not an empty object
Popular Posts
How to Check React Version Installed in Your System by Checking PACKAGE.JSON File and Executing Command from COMMAND PROMPT
- Get link
- X
- Other Apps
Execution failed for task ':react-native-reanimated:externalNativeBuildDebug' solved
- Get link
- X
- Other Apps
Error: Invalid `prisma.$executeRaw()` invocation: fixed using log: ["query"] option
- Get link
- X
- Other Apps
app keeps stopping error solved in android studio emulator using logcat in react native project
- Get link
- X
- Other Apps
ERROR Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined
- Get link
- X
- Other Apps