Navigate back to the homepage

how to check undefined in react js?

Infinitbility
React
August 5th, 2022 · 1 min read
how to check undefined in react js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To check or handle undefined in react js, use the if statement it will go inside in if statement if the value is not undefined.

Basically, If the statement will handle all your false values like 0, false, "", undefined, etc.

1let value = undefined;
2
3if(value){
4 console.log("value have some data")
5} else {
6 console.log("value is undefined")
7}

Today, I’m going to show you How do I check or handle undefined in react js, as above mentioned, I’m going to use the if statement to check value is undefined or has some data.

Let’s start today’s tutorial how do you check or handle undefined in react js?

React js check undefined example

Here, we will take the example of two states one state has some data and another has an undefined value, we will check both state console the undefined state name.

1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [str1] = useState("Infinitbility");
4 const [str2] = useState(undefined);
5
6 useEffect(() => {
7 if (str1) {
8 console.log("str1 have data");
9 } else {
10 console.log("str1 is undefined");
11 }
12
13 if (str2) {
14 console.log("str2 have data");
15 } else {
16 console.log("str2 is undefined");
17 }
18 }, []);
19
20 return (
21 <div className="App">
22 <p>{str1 ? "str1 have data" : "str1 is undefined"}</p>
23 <p>{str2 ? "str2 have data" : "str2 is undefined"}</p>
24 </div>
25 );
26}

In the above example, I have also put the example check undefined in render code here to check undefined we can use the ternary operator.

I hope it helps you, All the best 👍.

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 remove duplicates from array in react js?

how to remove duplicates from array in react js?

An example of remove duplicates from array in react js with the step-by-step guide.

August 4th, 2022 · 1 min read
how to remove duplicates from array in react native?

how to remove duplicates from array in react native?

An example of remove duplicates from array in react native with the step-by-step guide.

August 4th, 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]