Open In App

JavaScript Check the existence of variable

Last Updated : 07 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript has a built-in function to check whether a variable is defined/initialized or undefined. To do this, we will use the typeof operator. The typeof operator will return undefined if the variable is not initialized and the operator will return null if the variable is left blank intentionally.
Note: 

  • The typeof operator will check whether a variable is defined or not.
  • The typeof operator doesn’t throw a ReferenceError exception when it is used with an undeclared variable.
  • The typeof null will return an object. So, check for null also.

Example 1: This example checks whether a variable is defined or not. 

Javascript




let GFG_Var;
 
if (typeof GFG_Var === 'undefined') {
    console.log("Variable is Undefined");
}
else {
    console.log("Variable is defined and"
        + " value is " + GFG_Var);
}


Output

Variable is Undefined

Example 2: This example also checks whether a variable is defined or not. 

Javascript




let GFG_Var = "GFG";
 
if (typeof GFG_Var === 'undefined') {
    console.log("Variable is Undefined");
}
else {
    console.log("Variable is defined and"
        + " value is " + GFG_Var);
}


Output

Variable is defined and value is GFG

Example 3: In this example, we will check whether a variable is null or not. 

Javascript




let GFG_Var = null;
 
if (typeof GFG_Var === null) {
    console.log("Variable is null");
}
else {
    console.log("Variable is defined and value is "
        + GFG_Var);
}


Output

Variable is defined and value is null


Similar Reads

How to check the existence of URL in PHP?
Existence of an URL can be checked by checking the status code in the response header. The status code 200 is Standard response for successful HTTP requests and status code 404 means URL doesn't exist. Used Functions: get_headers() Function: It fetches all the headers sent by the server in response to the HTTP request. strpos() Function: This funct
2 min read
How to check the existence of key in an object using AngularJS ?
Given an object containing a (key, value) pair and the task is to check whether a key exists in an object or not using AngularJS. In order to check the existence of a key in an object, we will create an object having properties in the form of a key: value pair. Define a temporary variable that will hold the initial key of an object. Create a functi
3 min read
variable === undefined vs. typeof variable === “undefined” in JavaScript
Undefined comes into the picture when any variable is defined already but not has been assigned any value. Undefined is not a keyword. A function can also be undefined when it doesn't have the value returned. There are two ways to determine if a variable is not defined, Value and type. var geeks; alert ( geeks === undefined) It's so clear that you
2 min read
How to check a variable is of function type using JavaScript ?
A function in JavaScript is the set of statements used to perform a specific task. A function can be either a named one or an anonymous one. The set of statements inside a function is executed when the function is invoked or called. A function can be assigned to a variable or passed to a method. var gfg = function(){/* A set of statements */}; Here
3 min read
How to check the type of a variable or object in JavaScript ?
In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type of value. The type of the value assigned to a varia
2 min read
Check if a variable is a string using JavaScript
In this article, we will check if a variable is a string using JavaScript. We can check the variable by using many methods. Below are the following methods through which we can check the type of variable is a string: Table of Content Using typeOf OperatorUsing Instanceof OperatorUnderscore.js _.isString()Using Lodash _.isString() MethodApproach 1:
2 min read
How to Check if a Variable is of Type Number in JavaScript ?
In JavaScript, you can check if a variable is of type number using the typeof operator. The typeof operator returns a string indicating the type of the operand. Example: Here, typeof myVariable evaluates to the string 'number', and the === operator is used to check if it is equal to the string 'number'. If the condition is true, it means that the v
1 min read
How to check if a variable is an array in JavaScript?
This article will show you how to check whether the given variable value is an array or not. Table of Content Using JavaScript isArray() methodUsing JavaScript instanceof operatorChecking the constructor property of the variableMethod 1: Using JavaScript isArray() method isArray() method checks whether the passed variable is an Array object or not.
4 min read
How to check if a Variable Is Not Null in JavaScript ?
Our focus lies on understanding how to verify if a variable is not null in JavaScript. We'll explore several methods to tackle this issue effectively. These are the following ways to solve this problem: Table of Content Using if elseUsing Lodash _.isNull() MethodUsing the typeof Operator:Method 1: Using if elseIn this approach, we will use the if-e
3 min read
PHP | Check if a variable is a function
To determine whether a passed argument is a function or not, Few of the most preferred methods are shown below. Using is_callable() Function: It is an inbuilt function in PHP which is used to verify the contents of a variable in called as a function. It can check that a simple variable contains the name of a valid function, or that an array contain
3 min read