JavaScript - Different Between Null and Undefined


JavaScript is one of the most popular programming language used by various developers. JavaScript works on the Web by adding functionality to a website. These days JavaScript is not only used on the web, it also used for Developing Softwares, Mobile Apps, Developing Games, for Argument Reality and other AI related.

Many a times we often get confused on whats the difference between UNDEFINED and NULL. You'll get to know the difference.

So, it is likely possible that lot of JavaScript beginners confused null to undefined. Don't worry, that isn't a big deal since we all are learning.

In JavaScript:

undefined means a variable has been declared but has not yet been assigned a value.

When a Variable is declared but no assigned, then it I undefined. For example:

 let age; // declared but not assigned
   alert (age); // output is undefined

Below, a variable was declared which is age but was not assigned to any value. The output will then be undefined. The Code above states that the age is undefined.


On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value.

In JavaScript Null means "Value Unknown", or "empty". Null is a special type of and assignment that can be assigned to a variable. When null is assigned to a variable in JavaScript, it means the variable is assigned to an unknown value.

let age = null; // variable assigned to null
  alert(age); // output is null

Here you declared a variable "age" and assigned it to the value of null. Which means the value is unknown or empty. The above code will then output null since the variable is assigned to a value unknown.

Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object. Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.

Share this Article if you find it helpful. Follow up on twitter @moralistfestus[]

No Comments Yet