How to use for Loop and ‘===’ Operator In Javascript
In this approach, we are using a for loop to iterate through each key in the JSON object and checking if its corresponding value is null using the strict equality operator ===, setting a flag to true if a null value is found.
Syntax:
for (let key in object) {
if (object[key] === value) {
// code
}
}
Example: The below example uses for Loop and ‘===’ Operator to check if the json key value is null in JavaScript.
const jsonData = {
key1: null,
key2: "value2",
key3: undefined,
};
let res = false;
for (let key in jsonData) {
if (jsonData[key] === null) {
res = true;
break;
}
}
if (res) {
console.log(
"Null value is present in the JSON object.");
} else {
console.log(
"No null value found in the JSON object.");
}
Output
Null value is present in the JSON object.
How to Check if JSON Key Value is Null in JavaScript ?
In JavaScript, the JSON Object can contain null values, which should be identified. The below approaches can be used to check if the JSON key is null.
Table of Content
- Using for Loop and ‘===’ Operator
- Using Object.values() with Array.prototype.includes()
- Using Array.prototype.some()
Contact Us