How to Check if the Variable Exists or Defined in JavaScript

Definition and Usage

You can use the typeof( ) function to check the existence of variables and if it is defined in the JavaScript. The typeof( ) in JavaScript allows to look after the data type of its concerned ‘operand’. Here it is checked if the variable is numeric, string or undefined. The typeof operator returns the string, which recognizes data type of an expression. Here the ‘expression argument’ is any of the expressions for which typeof information is sought out.

Here is an example that will show how to check the existence of a variable or if it is defined in JavaScript.

Example

<script>

    var x;

    var y = 10;

    

    if(typeof x !== 'undefined'){

        // this statement will not execute

        alert("Variable x is defined.");

    }

    

    if(typeof y !== 'undefined'){

        // this statement will execute

        alert("Variable y is defined.");

    }

    

    // Attempt to access an undeclared z variable

    if(typeof z !== 'undefined'){

        // this statement will not execute

        alert("Variable z is defined.");

    }

    

    /* Throws Uncaught ReferenceError: z is not defined,

    and halt the execution of the script */

    if(z !== 'undefined'){

        // this statement will not execute

        alert("Variable z is defined.");

    }

    

    /* If the following statement runs, it will also

    throw the Uncaught ReferenceError: z is not defined */

    if(z){

        // this statement will not execute

        alert("Variable z is defined.");

    }

</script>

In the above example, it can be seen that by using typeof operator, you will come to know if the variable is defined or undefined. It will never show the ‘ReferenceError’ unless it is used with an ‘undeclared variable’. You can use the ‘defined test’ to see if the variable really exists in JavaScript that will provide the same outcome as in the example above where ‘Variable z’ is defined.

The typeof( ) will help them to know whether a variable has been initialized, which means that it has been assigned some value or declared while writing in the JavaScript. However, if he makes a mistake then the variable may remain undefined.

The typeof( ) function of JavaScript helps in checking out variables and defining the script by carrying out few expressions as:

  • It checks out whether the variable has been declared or is still undefined.
  • The typeof operator checks out the missing parameters, uninitialized variables and non-existent properties.
  • While writing the variable in JavaScript the developer must see that the value is neither null nor undefined. The introduction of ‘default operator’ will also tell about the ‘default value’ or in case ‘myValue’ is not defined
  • Apart from checking the existence of variables, other typeof values that are returned include the ‘object’, ‘Boolean’, ‘function’, ‘number’, ‘string’ and ‘undefined’ values.

While writing the script, the parentheses can be optional in the ‘typeof’ syntax.

View Demo

↑ Back to Top