In this post we are going to learn How to check all object property is null in JavaScript by using some function of javascript. The JavaScript array. every() and array.some() functions are used to check for null values, We will get all the keys with null empty, and undefined values from an object.
1. How to check all object property is null JavaScript
The array. every() method calls a function for every element of the array. If all the elements satisfy the condition set by the function then an array.every() will return True else return False if any element of the array does not satisfy the condition. For an empty array, this function always returns False.
- To check all object properties is a null array, the First we will call object. values() to get an array of all values of an object.
- The array. every() method will run a function to check if each element is ‘Null’
- if all the values are ‘Null’ then an array.every() method will return True
let Employee =
{Empname: null, salary: null,EmpID:null,Dep:null}
const isObjnull = Object.values(Employee).every(value => {
if (value === null) {
return true;
}
return false;
});
console.log(isObjnull)
Output
True
2. check all object property is null or empty
In this Javascript program that object values() to get all the values of object as array.The array.every() method execute a callabck function for each element of array that check multiple condition where values is (value === null || value === undefined || value === ”) if all elements satisfy the condition then it will return True else False.
let Employee =
{Empname: null, salary: null,EmpID:null,Dep:null,EmpID:undefined,EmpProj:""}
const isObjnull = Object.values(Employee).every(value =>{
if (value === null || value === undefined || value === '') {
return true;
}
return false;
});
console.log(isObjnull)
Output
true
3. How to check all object property is null or empty
The Javascript array.some() function check if one of array element satisfying the given condition set by array.some() function then return true else return False .In this javascript program the array.some() function is if the object property is empty and not null.
let Employee =
{Empname: null, salary: null,EmpID:null,Dep:null,EmpID:undefined,EmpProj:' '}
const isObjnull = !Object.values(Employee).some(val => (val !== null && val !== ' '));
console.log(isObjnull)
Output
False
4. Get key object is null or empty
Sometimes we have to need to get all the keys of objects that have undefined, null, and empty values, First, we will get all object keys as an array using the object. keys().
- The array. filter() method will execute a callback function for each element of the array to check for a condition where values are undefined, null, or empty.
- It returns an array of keys that satisfies the condition this is how we get if the keys have undefined, null, empty values.
let Employee =
{Empname: null, salary: null,EmpID:null,Dep:null,EmpID:undefined,EmpProj:""}
const keys= Object.keys(Employee).filter((key)=> {
if (Employee[key] === "" || Employee[key]===undefined || Employee[key]===null) {
return key
}
});
console.log(keys)
Output
[ 'Empname', 'salary', 'EmpID', 'Dep', 'EmpProj' ]
5. Function to check all object property is null JavaScript
In this Javascript program example, we defined a function to check if the values of objects are null or empty. We have defined a boolean variable and initialed it to True.They are checking if Employee[key] === null || Employee[key] === “” using the strict equality operator.Let us understand with the below program
let Employee =
{Empname: null, salary: null,EmpID:null,Dep:null,EmpID:undefined,EmpProj:""}
function checkforNull(Employee) {
var isnul = true;
for (var key in Employee) {
if ( !( Employee[key] === null || Employee[key] === "" ) ) {
isnul = false;
break;
}
}
return isnul;
}
console.log(checkforNull(Employee))
Output
false
Check object property for Falsy values
The falsy values in javascript ‘false’,’null’,’undefined’,’NaN’.Let us understand how to check for Falsy values in object in JavaScript.
let Employee =
{Empname: null, salary: 0,EmpID:undefined,Dep:null,EmpID:false,EmpProj:""}
const isFVal = Object.values(Employee).every(value => {
if (!value) {
return true;
}
return false;
});
console.log(isFVal);
Output
True
Summary
In this post we have learned how to check all object property is null JavaScript with examples by using array.every() and array.some() function and also get all key which has null empty and undefined values