How to Check if JSON Key Value is Null in JavaScript ?
Last Updated :
16 Apr, 2024
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.
Using for Loop and '===' Operator
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.
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.");
}
OutputNull value is present in the JSON object.
Using Object.values() with Array.prototype.includes()
In this approach, we are using Object.values() to extract an array of values from the JSON object, and then we use Array.prototype.includes() to check if the array includes null, indicating the presence of a null value in the JSON object.
Syntax:
const valuesArray = Object.values(object);
if (valuesArray.includes(value)) {
// code
}
Example: The below uses Object.values() with Array.prototype.includes() to check if the json key value is null in JavaScript.
JavaScript
const jsonData = {
key1: null,
key2: "value2",
key3: undefined,
};
const res = Object.values(jsonData);
if (res.includes(null)) {
console.log(
"Null value is present in the JSON object.");
} else {
console.log(
"No null value found in the JSON object.");
}
OutputNull value is present in the JSON object.
Using Array.prototype.some()
In this approach, we are using Object.values() to extract an array of values from the JSON object, and then we use Array.prototype.some() with a callback function to check if any value in the array is null, indicating the presence of a null value in the JSON object.
Syntax:
const hasValue = array.some(function(item) {
return item === value;
});
if (hasValue) {
// code
}
Example: The below example Array.prototype.some() to check if the json key value is null in JavaScript.
JavaScript
const jsonData = {
key1: null,
key2: "value2",
key3: undefined,
};
const res = Object.values(jsonData).
some(value => value === null);
if (res) {
console.log(
"Null value is present in the JSON object.");
} else {
console.log(
"No null value found in the JSON object.");
}
OutputNull value is present in the JSON object.
Similar Reads
How to check if a Variable Is Not Null in JavaScript ? In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
4 min read
How to Check if Object is JSON in JavaScript ? JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows: Table of Content Using Constructor Type Ch
2 min read
How to check if a value is object-like in JavaScript ? In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 min read
How to check for null values in JavaScript ? The null values show the non-appearance of any object value. It is usually set on purpose to indicate that a variable has been declared but not yet assigned any value. This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is becau
4 min read
How to check if the value is primitive or not in JavaScript ? To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
3 min read