What is the Javascript null Data Type?

By: Dusty Arlia
Published on Monday, March 25, 2013, 10:18 PM
Last Updated on Sunday, July 12, 2015 at 3:24 PM
Total Updates: 3

The JavaScript null data type has only one value: null. The data type and value of null represents nothing. If you want to erase the value of a variable, set its value to null. This will empty the variable without deleting it. null evaluates to false. Here is an exampe:

var null_variable = null;
if (null_variable) {
    document.write("evaluates to true");
} else {
    document.write("evaluates to false");
}

After the code above runs, you get the message evaluates to false printed to your screen.

Any variable that gets the value null assigned to it, or has no valid Number, String, Boolean, Array, or Object, gets the data type null. For backwards compatibility reasons, the typeof operator in JavaScript reports null values as being of type object, not of type null. For example:

var null_variable = null;
alert(typeof (null_variable));

In the code above, the variable null_variable gets the value null assigned to it. The alert() method pops up an alert box with the word object in it. That is because null_variable is reported as having the data type object.

Do not use the null keyword as the name of any of your variables or functions.

null is not the same as 0 which is used in other programming languages (C and C++).

You can compare the null data type to:

Comments:

Ad: