This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

Difference Between Null and Undefined


In this tutorial post we are going to discuss the following datatype keyword Null and Undefined. We will see the difference between these datatypes when it comes in use. Before that we'll undersatand concept of null, undefined, zero, Non-Zero. Here we'll see how they are differ to each other. We'll explain that with a example of toilet tissue holder.

  • Non-zero value - It is like a holder with roll of toilet tissue and there's tissue still on the tube.
  • Zero value - It is like a holder with an empty toilet tissue tube.
  • Null value - It is like a holder that doesn't even have a tissue tube.
  • Undefined value - It is similar to the holder itself being missing.

TypeScript

Null Keyword

Null is a special type in TypeScript that is used to define absence of a value or it is an empty value.Variable declared and is an empty value. It accepts only one value which is null. The variable to represent no value it is an object.

Example 

Suppose we are developing an application and showing employee list with first, middle and last name. So many employees don't have a middle names. So in such a case its better to assign the value null to the middle name variable.

Example

// Null Keyword
var CheckNull = null; 
console.log(CheckNull); //output- null  
console.log(typeof(CheckNull)); //output- object 
CheckNull=0;  // Assigned Zero
console.log(CheckNull); //output- Zero 
let Addnumber:number=CheckNull + 8 // Premitive operation
console.log(Addnumber); //output- Zero  

Undefined Keyword

Undefined is also a special type in TypeScript that is used to define missing of a value or it is an empty value. Variable has been given a space in memory, but no value is assigned to it. This type we can not used as assign value.

 Example

Suppose we are developing an application and showing employee list with first, middle and last name. So many employees don't have a middle names. So in such a case middle name variable of your person object and it has the value undefined. He wouldn't know if the developer forgot to initialize this variable.

Example

//Undefined Keyword
var UndefinedNull; 
console.log(UndefinedNull); //output- undefined  
console.log(typeof(UndefinedNull)); //output- undefined 
UndefinedNull=NaN;  // Assigned Zero 
console.log(UndefinedNull); //output- Zero 
let UndefinedAddnumber:number=CheckNull + 8 // Premitive operation
console.log(UndefinedAddnumber); //output- Zero 

Null vs Undefined

The main difference between Null and Undefined are as following:

SNo. Null Undefined
1 Null must be assigned to a variable. The default value of any unassigned variable is undefined.
2 Null is the intentional absence of a value Undefined is the unintentional absence of a value
3 It is an object type. it is type undefined.
4 Null is converted to zero while performing primitive operations. Undefined is converted to NaN while performing primitive operations.
5 Null is a valid value in JSON. You can represent undefined as a JSON (JavaScript Object Notation).

Prev Next