HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TypeScript / undefined vs null in JavaScript

undefined vs null in JavaScript

A variable is said to be “undefined” if it has been declared but not initialized. Whereas “null” is assigned to a variable whose value is absent at that moment.

In simple words, when you do not assign value to a variable, JavaScript engine treats it as undefined. The null value is assigned by programmer to indicate that variable has nothing inside of it, but intend to have value later in program execution.

undefined

See below JavaScript statements to understand the undefined.

var myVar;		//Variable declaration without assigning any value to it

console.log( myVar );	//undefined

console.log( typeof(myVar) );	//undefined

console.log( undeclaredVar );	//Uncaught ReferenceError: undeclaredVar is not defined

If you see a variable with value in undefined, then you know that:

  1. The variable has been declared in the program.
  2. There is no value assignment done for the variable.
  3. If you check the type of variable, it will be undefined. Please note that is one of primitive types [string, number, boolean, null, undefined, symbol (new in ES6)] in JavaScript.
  4. If a function returns undefined, it didn’t returned any value.

undefined is not reserved keyword in JavaScript.

null

null represents intentional absence of value. Conceptually, it’s very much like null in other programming languages like Java, C# etc. null expresses a lack of identification, indicating that a variable points to no object.

var myVar = null;		//Variable declared and assigned to null

console.log( myVar );	//null

console.log( typeof(myVar) );	//object

Please note that null is of type object with a valid value, no properties, is non-mutable, and only a single instance of the same exists in the system at all times.

Using equality operators with null and undefined

Always remember to use strict equality operator ( === ) for comparing variables with null and undefined, when you want to clearly distinguish between null and undefined variables.

Read More: == vs === in JavaScript

var myUndefinedVar;			//undefined
var myNullVar = null;		//null

myUndefinedVar == myNullVar;		//true - which not correct as both are different	

myUndefinedVar === myNullVar;		//false - correct behavior

null evaluates to zero

One major difference between null and undefined is in the way they are converted to primitive types.

  • null is converted to zero (0) while performing primitive operations.
  • undefined is converted to NaN.
var myUndefinedVar;			//undefined
var myNullVar = null;		//null

myUndefinedVar + 100;		//NaN

myNullVar + 100;			//100

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Dinesh

    November 5, 2018

    Very good article, but according to the documentation null is in fact a primitive data type in Javascript, not an object.

Comments are closed on this article!

Search Tutorials

TypeScript Tutorial

  • TypeScript – Introduction
  • TypeScript – Types
  • TypeScript – Union Types
  • TypeScript – String Literal Types
  • TypeScript – var, let and const
  • TypeScript – Template Strings
  • TypeScript – Arithmetic Operators
  • TypeScript – Logical Operators
  • TypeScript – Comparison Operators
  • TypeScript – ‘for…of’ Loop
  • TypeScript – Spread Operator
  • TypeScript – Arrays
  • TypeScript – Enums
  • TypeScript – Map
  • TypeScript – Set
  • TypeScript – Functions
  • TypeScript – Function Overloading
  • TypeScript – Transpiler
  • TypeScript – Truthy and falsy
  • TypeScript – == vs ===
  • TypeScript – undefined vs null
  • TypeScript – Variable Hoisting
  • TypeScript – tsconfig.json

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces