How do you know if a variable is not null or un.defined?

                                                        

   On - 11 Feb, 2023
   By - Apu Gorai
One way to determine if a variable is not null or un.defined is to use the JavaScript logical operator OR (||). This operator will evaluate to true if either of its operands is true. For example, if a variable is not null, the expression (variable || false) will evaluate to true.
Another way to check if a variable is not null or un.defined is to use the typeof operator. For example, if a variable is not null, the expression (typeof variable !== 'un.defined') will evaluate to true.
Third way to check if a variable is not null or un.defined is to use the comparison operator !=. This operator will return true if the operands are not equal. For example, if a variable is not null, the expression (variable != null) will evaluate to true.
<script>
   console.log(myVar != null);
</script>
In addition to these methods, you can also check if a variable is not null or un.defined by using the strict equality operator ===. This operator will return true if the operands have the same value and type. For example, if a variable is not null, the expression (variable === null) will evaluate to false.
In addition to these methods, you can also check if a variable is not null or by using the if statement. This statement will execute the specified block of code if the specified condition is true.
<script>
 let myVar;
 if(myVar != null) { 
    alert('Variable has value.');
   }
   else{
     alert('Variable has no value.')
   }
</script>
<script>
 let myVar;

 if(myVar != null && myVar != un.defined){  
    console.log("myVar has some value.");
  } else {
    console.log("myVar is un.defined.");
  } 
 // output => myVar is un.defined.
</script>
In this article, you have learned how do you know if a variable is not null or ?
Load More »