Navigate back to the homepage

how to check null value in json object in javascript?

Infinitbility
JavaScript
July 31st, 2022 · 1 min read
how to check null value in json object in javascript?

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.

1// Using if statement
2if(obj.value){
3 console.log("Value have data")
4} else {
5 console.log("Value is null or falsy value")
6}
7
8// ternary opertor
9obj.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.

1if(obj.value == null){
2 console.log("Value is null")
3} else {
4 console.log("Value is not null")
5}

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.

1let obj = {
2 name: null
3}

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.

1let obj = {
2 name: null
3}
4
5if(obj.name){
6 console.log("name have data")
7} else {
8 console.log("name is null")
9}
10
11// 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.

1let obj = {
2 name: null
3}
4
5obj.name ? "name have data" : "name is null" ;
6
7// Output
8// 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.

1let obj = {
2 name: null
3}
4
5if(obj.name == null){
6 console.log("name is null")
7} else {
8 console.log("name is not null")
9}
10
11// Output
12// name is null
13
14``
15
16I hope it helps you, All the best 👍.
17
18export const _frontmatter = {"title":"how to check null value in json object in javascript?","author":"Infinitbility","date":"2022-07-31T00:00:00.000Z","hero":"./images/template.jpg","subscription":true,"excerpt":"An example of checking null value in JSON object in javascript with step by step guide.","categories":["JavaScript"]}

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to set value in json object in javascript?

how to set value in json object in javascript?

An example of the set value in JSON object in javascript with step by step guide.

July 30th, 2022 · 1 min read
how to check if a string contains only whitespace in typescript?

how to check if a string contains only whitespace in typescript?

TypeScript, check if a string contains only whitespace example with a step-by-step guide.

July 28th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]