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

Undefined comes into a 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

var geeks;
alert ( geeks === undefined)

chevron_right


It’s so clear that you assigned a variable that was not defined but the variable exists. Here you are comparing geeks variable with the global variable “undefined” which is undefined also.
Syntax:

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

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

var firstName;
var lastName = null;
  
// Print: undefined        
console.log(firstName); 
// Print: null
console.log(lastName);  
          
// 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');
     }

chevron_right


Output:

undefined
null

undefined
object

false

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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.