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.

You can declare a variable of the above types according to the requirements. Sometimes in code, you require to check the type of a variable, and based on the result, some further actions are taken. 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.

For 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

What is the undefined type of variable?

Ans. 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.

For example:

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

Output:

undefined

What is the null type of variable?

Ans. 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.

For 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.

Null type is used to show that a variable is defined and also 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 the value of the variable is undefined or null. Based on the result, further actions are taken, such as it executes 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.

For example

var x = “Errorsea”;

Then it will show the following output.

Output:

Variable is not undefined or null!

Method 2:

In this method, you directly assign the value to 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 false value. You put the value in the condition of 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 the methods for 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 *