Follow @endyourif rss Facebook LinkedIn

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

Published on Jul 25, 2022

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.

Tags: JavaScript

About Jamie Munro

Welcome to my blog. I have been compiling articles since 2009! Below is a list of every post I've ever created. Some of my major focuses have been compiling the best source of jQuery tutorials on the Internet today. A recent focus of mine is now focusing on creating the best SQL Tutorials for Beginners, Intermediate and Advanced Users. This is a compiliation of all my experience about how to do common SQL commands to optimize for performance. As you might see from the links around here, I've also written and published 5 books. My first and last book are focused around ASP.NET MVC and Web API tutorials. The last book has a big focus on integrating Knockout.js as the MVVM for the front-end code. O'Reilly Media was so interested in Knockout.js that they actually commissioned me to write that book first as it is one of the only books dedicated entirely to this MVVM library. So if you're looking for the best Knockout.js tutorials, I've compiled the best list. My first book, even though it was self published, was on the PHP framework: CakePHP. I used this framework for years and many of my first ever exampes were all focused on CakePHP tutorials that provide so many fantastic PHP examples. There are so many Javascript tutorials out there, but I noticed that one of the things I work on most with Javascript is arrays. With this I've worked hard on compiling a big list of really advanced Javascript array examples. These array examples are amazing including how to group by, get distinct array elements and so many more. I've also dabbled a little bit with Node.js and during those experiments I definitely ran into some common errors like Can't set headers after they are sent, Solving No Access-Control-Allow-Origin with Node js and Express, and Uncaught ReferenceError: require is not defined. With these common errors I've compiled a list of Node.js tutorials that help solve these problems, but more importantly getting you started with the basics.