In JavaScript, variables are of different types, such as
Index
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
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
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
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
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:
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!
In this method, you directly assign the value to the if condition.
The following types are considered as false.
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!
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 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…