Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To check the null value in a JSON object in javascript, Just use the if statement or ternary operator before using the JSON object value. it will handle all falsy values with null.

JavaScript if statement or ternary opertor will handle (""), null, undefined, false and the numbers 0 and NaN.

Let’s take a short example check the null value in JSON object in javascript.

// Using if statement
if(obj.value){
    console.log("Value have data")
} else {
    console.log("Value is null or falsy value")
}

// ternary opertor
obj.value ? "Value have data" : "Value is null or falsy value" ;

Or if want to check only null values in a JSON object, you can compare your object value with the null datatype. like the below example.

if(obj.value == null){
    console.log("Value is null")
} else {
    console.log("Value is not null")
}

Today, I’m going to show you How do I check the null value in JSON object in javascript, as above mentioned, I’m going to use if statement or ternary operator to validate object value is null or not.

Let’s start today’s tutorial how do you check the null value in JSON object in javascript?

Let’s create a sample object with null data and apply all the above-mentioned methods to it.

let obj = {
    name: null
}

Check null using the if statement

To check null in if statement directly mention key in if statement it will go inside else method if object value contains any false value.

let obj = {
    name: null
}

if(obj.name){
    console.log("name have data")
} else {
    console.log("name is null")
}

// output: "name is null"

Check null ternary opertor

To check null in ternary opertor mention object key in the ternary statement if key contains null it will print name is null.

let obj = {
    name: null
}

obj.name ? "name have data" : "name is null" ;

// Output
// name is null

Check only null in a JSON object

To check only the null value in a JSON object, we will compare it with the null in the if statement. if the value is null it will print a null value.

let obj = {
    name: null
}

if(obj.name == null){
    console.log("name is null")
} else {
    console.log("name is not null")
}

// Output
// name is null

I hope it helps you, All the best đź‘Ť.