How to Check the Variable of Type Undefined or Null in JavaScript

In JavaScript, variables are of different types, such as

  • Boolean
  • Undefined
  • Null
  • Number
  • String
  • Object
  • Array, etc.

Variable of Type Undefined or Null in JavaScript

We can declare a variable of the above types according to the requirements. Sometimes in code, we require to check the type of a variable, and based on the result, some further actions are taken.

Here, we have explained here how to check undefined or null values in JavaScript. 

Note: typeof() function is used for checking the type of the variable. It is the inbuilt function of JavaScript.

Example

<script>
  var x = 10;
  var m = "Errorsea";
  var y = typeof(x);
  var n = typeof(m);
  document.write(y+"<br>");
  document.write(n);
</script>

Output

Number

string

The Undefined Type of Variable

When you declare a variable in your script but does not assign the value to that variable. Then that variable is of the type undefined.

Example

<script>
  var x;
  var y = typeof(x);
  document.write(y+"<br>");
</script>

Output

undefined

The Null Type of Variable

When you declare a variable in your script, and you assign the value null to that variable. It means it has no value. Then that variable is of the type object.

Example

<script>
  var x = null ;
  var y = typeof(x);
  document.write(y+"<br>");
</script>

Output

object

Note: When you only define the variable, then it is of an undefined type, and when you assign a null value to a variable, then it is of the object type.

The null type shows that a variable is defined and has no value to that variable, whereas the undefined type doesn’t have value.

Read Also: JavaScript String Methods

How To Check the Variable of Type Undefined or Null?

There are different methods for checking whether the variable’s value is undefined or null. Based on the result, further actions are taken, such as executing the particular block of statements the methods used for checking the null or undefined types are as follows:

Method 1

The simplest method to check the undefined or null value is the comparison method. In this method, the typeof() function is used.

Example

<script>
  var x;
  if(typeof(x) == "object" || typeof(x) == "undefined"){
    document.write("Variable is undefined or null !<br>");
  }else{
    document.write("Variable is not undefined or null !<br>");
  }
</script>

Output

Variable is undefined or null!

If we assign some value to that variable.

Example 2

var x = "Errorsea";

Then it will give the following output.

Output

Variable is not undefined or null!

Method 2

In this method, you directly assign the value to the if condition.
The following types are considered as false.

  • Null
  • Undefined
  • NaN
  • Empty string (“”)
  • Zero (0)

So, we first check the type of a variable, and then if the value is of the above type, it has a false value. You put the value in the condition of the if statement. Based on the result, further actions are taken.

Example

<script>
var x;
var y = typeof(x);
if(!y){
document.write("Variable is not undefined or null !<br>");
}else{
document.write("Variable is undefined or null !<br>");
}
</script>

Output

Variable is undefined or null!

Conclusion

This is all about checking the value of the variable is null or undefined. It is mainly used in forms when we want to check the input value is undefined or null.

I hope you found this post fully informative and helpful.

Thank you for reading 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
php array foreach loop
What Is the PHP Array Foreach Loop and How It Is Different From for Loop

In PHP language, there are different types of arrays like an indexed array, associative array, and multidimensional array. We use...

Close