null and undefined in JavaScript:

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. A null means absence of a value.

Example: null

var myVar = null;

alert(myVar); // null 

In the above example, null is assigned to a variable myVar. It means we have defined a variable but have not assigned any value yet, so value is absence.

null is of object type e.g. typeof null will return "object"

If you try to find DOM element using document.getElelementByID for example, and if element is found then it will return null. So it is recommended to check for null before doing something with that element.

Example: null

var saveButton = document.getElementById("save");

if (saveButton !== null)
        saveButton.submit();

A null value evaluates to false in conditional expression. So you don't have to use comparison operators like === or !== to check for null values.

Example: null in conditional expression

var myVar = null;

if (myVar)
    alert("myVar is not null');
else
    alert("myVar is null" );

undefined:

Undefined is also a primitive value in JavaScript. A variable or an object has an undefined value when no value is assigned before using it. So you can say that undefined means lack of value or unknown value.

Example: undefined

var myVar;

alert(myVar); // undefined 

undefined is a token. typeof undefined will return undefined not an object.

In the above example, we have not assigned any value to a variable named 'myVar'. A variable 'myVar' lacks a value. So it is undefined.

You will get undefined value when you call a non-existent property or method of an object.

Example: undefined

function Sum(val1, val2)
{
    var result = val1 + val2;
}

var result = Sum(5, 5);
alert(result);// undefined 

In the above example, a function Sum does not return any result but still we try to assign its resulted value to a variable. So in this case, result will be undefined.

If you pass less arguments in function call then, that parameter will have undefined value.

Example: undefined

function Sum(val1, val2)
{
    return val1 + val2;  // val2 is undefined 
}

Sum(5);

An undefined evaluates to false when used in conditional expression.

Example: undefined in conditional expression

var myVar;

if (myVar)
    alert("myVar evaluates to true");
else
    alert("myVar evaluates to false");

null and undefined is one of the main reasons to produce a runtime error in the JavaScript application. This happens if you don't check the value of unknown return variables before using it. If you are not sure that a variable will always have some value, the best practice is to check the value of variables for null or undefined before using them.

Points to Remember :

  1. null and undefined are primitive values in JavaScript.
  2. A null value means absence.
  3. An undefined value means lack of value.
  4. A null or undefined value evalutes to false in conditional expression.