How To Check Undefined Variable In JQuery/JavaScript
In this tutorial, we will discuss How To Check Undefined Variable In JQuery/JavaScript. javascript default provides the typeof keyword it uses to check the data type of a variable. To check an undefined variable in jQuery or JavaScript, we can use one of these two methods. 1. Compare variable type to undefined
and 2. Compare variable to undefined.
The example to check the undefined variable using typeof.
Compare variable type to undefined
<script>
if(typeof var1 == 'undefined') {
alert("var1 is undefined");
}
if(typeof var2 === 'undefined') {
alert("var2 is undefined");
}
</script>
Compare variable to undefined.
This method to identify whether the variable is defined or not is to compare the variable to type undefined.
<script>
if(var1 == undefined) {
alert("var1 is undefined);
}
</script>