Open In App

variable === undefined vs. typeof variable === “undefined” in JavaScript

Last Updated : 19 Jan, 2023
Like Article

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 assigned a variable that was not defined but the variable exists. Here you are comparing the geeks variable with the global variable “undefined” which is undefined also. 


  • Check by Value (Strict equality Operator): Here you will get whether the variable is assigned a value or not if the variable is not assigned a value it will display undefined.
  • Check the type (Typeof operator): Here you will get what type of variable was that if there is no variable assigned then it will display “undefined”.

Note: The strict equality operator (===) doesn’t check whether the variable is null or not. The type of operator does not throw an error if the variable has not been declared.

Example: Here we assign two variables one is undefined and the other one is defined as “null”, here null is not undefined when you put console.log it will show “null” if you check typeof then it will display the object, below program will illustrate the approach more clearly. 


var firstName;
var lastName = null;
// Print: undefined        
// Print: null
// Print: undefined
console.log(typeof firstName);
// Print: object 
console.log(typeof lastName);  
// Print: false
console.log(null === undefined) 
if(firstName === undefined) {
    console.log('LastName is undefined');
} else if(firstName === null){
    console.log('FirstName is null');





variable === undefined VS typeof variable === “undefined” 

variable === undefined

typeof variable === “undefined”

Here the assigned variables don’t have any value but the variable exists. Here the type of variable is undefined.
If you assigned a value(var geeks === undefined ) it will show, if not it will also show undefined but in different meaning. Here the undefined is the typeof undefined.
In this, null will display if you assigned null to a variable, null is loosely equals to undefined But here typeof will show object.

Similar Reads

What is typeof Operator in JavaScript ?
The typeof operator in JavaScript is used to check the type of the operand passed to it. It will return the type of the passed operand that can be used to compare the types of the two different variables declared in the code. The typeof operator is used by specifying it just before the name of the operand to get its type. It returns the type of the
1 min read
JavaScript typeof Operator
In JavaScript, the `typeof` operator is used to determine the data type of its operand. It returns the data type as a string. The operand can be any object, function, or variable. This operator provides a convenient way to identify the type of data you are working with, facilitating effective data handling and manipulation in JavaScript programs. S
3 min read
What is a potential pitfall using typeof bar === "object" to determine if bar is an object ?
The typeof operator in JavaScript returns a string that indicates the data type of the operand, whether it be a variable, function, or object. Syntax: Following is the syntax of typeof operator: typeof operand // OR typeof(operand)Parameter: It takes the following parameter: operand: The expression whose type needs to be evaluated.Example: Basic ex
4 min read
TypeScript Narrowing typeof type guards
In this article, we are going to learn about Narrowing typeof type guards. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the typeof type guard allows you to narrow down a variable's type based on the result of the typeof operator. This is particularly useful when dealing with primiti
3 min read
How to get the first non-null/undefined argument in JavaScript ?
There are many occasions when we get to find out the first non-null or non-undefined argument passed to a function. This is known as coalescing. Approach 1: We can implement coalescing in pre-ES6 JavaScript by looping through the arguments and checking which of the arguments is equal to NULL. We then return the argument that is not NULL immediately
5 min read
JavaScript undefined Property
The undefined property is used to check if a value is assigned to a variable or not. Syntax: var x; if (typeof x === "undefined") { txt = "x is undefined"; } else { txt = "x is defined"; } Return Value: It returns 'defined' if the variable is assigned any value and 'undefined' if the variable is not assigned any value. More example code for the abo
1 min read
How to check empty/undefined/null string in JavaScript?
If we want to check any string either it is filled or empty we can use Javascript for such purpose for example : form validation. Example : C/C++ Code <script> function checkempty(form) { if ( == null || == undefined || == 0) { alert("Name cannot be empty\n"); return false; } els
3 min read
What are undeclared and undefined variables in JavaScript?
Undefined: It occurs when a variable has been declared but has not been assigned any value. Undefined is not a keyword. Undeclared: It occurs when we try to access any variable that is not initialized or declared earlier using the var or const keyword. If we use 'typeof' operator to get the value of an undeclared variable, we will face the runtime
1 min read
How to Replace a value if null or undefined in JavaScript?
In JavaScript if a variable is not initialised with any value, then it is set to undefined. We can set a default value if a value is undefined. This can be done using two ways. Example 1: By using if checks (Brute force). In this method, we will manually check whether a value is not null or not undefined, if so then set it to some default value. Ex
2 min read
JavaScript ReferenceError - Reference to undefined property "x"
This JavaScript warning reference to undefined property occurs if a script tries to access an object property that doesn't exist. Message: ReferenceError: reference to undefined property "x" (Firefox) Error Type: ReferenceError(Only reported by firefox browser) Cause of the error: The script is trying to access an object property that doesn't exist
2 min read