How to check for undefined in JavaScript How to check for undefined in JavaScript

It is very easy to confuse the difference between null and undefined. In this article I will go over how to use the typeof operator to see if a variable is declared or is not defined. Let's dive right in.


Table of contents:

JavaScript typeof operator

To get the current type of the value that the variable stores, you use the typeof operator:

let output = 120;
console.log(typeof (output)); // "number"
output = false;
console.log(typeof (output)); // "boolean"
output = "Hi";
console.log(typeof(output)); // "string"

In the examples above, if you run them the data type will be outputted as shown beside as a comment. Because a variable that has been assigned a value has been assigned a value and thus does not return null or undefined.

If you're looking for more articles, I highly suggest JavaScript String concat() Method.

Introduction to undefined and null values

In Javascript undefined is not the same as null. Undefined is a value that has not been assigned a value. Whereas null is a value for a variable that has been assigned a value of null.

In the previous examples using the typeof operator provided output for a variable has been defined. Now let's see how it differs the value is undefined.

console.log(typeof output); // "undefined"
let output = null;
console.log(typeof output); // "object"

In the first output I never created a variable has a value of. So output had never been declared so Javascript outputted undefined.

In the second output the typeof operator does not know what the type of output is even though it was defined unlike the earlier examples it the typeof operator new it was a string or a number, and even a boolean.

As we have seen in the variable section that we can assign any primitive or non-primitive type of value to a variable. JavaScript includes two additional primitive type values - null and undefined, that can be assigned to a variable that has special meaning null You can assign null to a variable to denote that currently that variable does not have any value but it will have later on.

Undefined vs Null - Javascript

undefined and null variables are easily confused, and sometimes use the terms interchangeably. Though, there is a difference between them:

  • undefined is a variable where the value of has not been assigned and the variable isn't defined to be anything because it has never been declared aka not defined.
  • null is a variable that is defined but never assigned a value.

What is type of javascript

Here is an example of using the if typeof to check for a variable is undefined.

if typeof output === undefined
	console.log('Undefined, please define a value');

Next if you want to check for null is very similar:

let output = null;
if typeof output === null
	console.log('Null, please define a value');

Is undefined == null?

A null with the global variable “undefined” which is undefined also. Syntax: 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. 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”. 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.

Finally if you're using Nodejs, this is a great example of Solving No Access-Control-Allow-Origin with Node js and Express.

Published on Jul 25, 2022

Tags: JavaScript

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.