Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu February 03, 2023 › #HowTo #Javascript

How do you know if a variable is not null or undefined?

In this article, we will talk about how to check if a variable is not null or undefined. I know this can be confusing to know when a variable is not null or undefined.

But it's important to understand the difference between these two types of values so that you can properly use them in your code.

Fortunately, there are several ways you can check if a variable is not null or undefined.

The easiest way to determine if a variable is not null or undefined is by using an if-else statement.

<script>
 let myVar;

 if(myVar != null && myVar != undefined){  
    console.log("myVar has some value.");
  } else {
    console.log("myVar is undefined.");
  } 
 // output => myVar is undefined.
</script>

By using this kind of logic we can quickly determine whether our variable is either not equal to both null and undefined.

save
listen
AI Answer
Write Your Answer
loading
back
View All