Compare the Difference Between Similar Terms

Difference Between null and undefined in JavaScript

Key Difference – null vs undefined in JavaScript
 

JavaScript is used as a client-side scripting language to make the web pages dynamic. It is easy to use with HyperText Markup Language (HTML). JavaScript is useful to increase interactivity and to build richer interfaces. When programming with JavaScript, it is necessary to store data. A variable is a storage area that the program can manipulate. Variables are memory locations. Each variable has a type. It depends on the range of values that can be stored in that memory location. JavaScript supports many data types. The primitive data types are Numbers, String and Booleans. Numbers store numerical values, Strings store a sequence of characters and Booleans store true or false. JavaScript also has composite data types that are objects etc. There are another two variables. They are null and undefined. This article discusses the difference between null and undefined in JavaScript. The key difference between null and undefined in JavaScript is that null is used to assign a non-value to a variable while undefined is used when a variable is declared but not assigned with a value.

CONTENTS

1. Overview and Key Difference
2. What is null in JavaScript
3. What is undefined in JavaScript
4. Similarities Between null and undefined in JavaScript
5. Side by Side Comparison – null vs undefined in JavaScript in Tabular Form
6. Summary

What is null in Javascript?

JavaScript supports Object Oriented Programming. A program or the software can be modeled into objects using JavaScript. These objects communicate with other objects. JavaScript objects are written using curly braces. Object properties are written as name, value pairs. They are separated by a comma. e.g. var student = {name: “Ann”, marks: 65};

When the programmer wants to assign a non-value to a variable, he can use the data type null. This data type is considered as an object.

Refer the below JavaScript statements.

var x = null;

document.write(x);

document.write(typeof(x));

The document.write(x) will give the value of x. It is null. The type of x is object.

Refer the below example.

var student = {name: “Ann”, marks: 65};

student = null;

According to the above, the value of student is null. Data type is object.

What is undefined in Javascript?

In JavaScript, when a variable is declared but didn’t assign a value, then it is undefined.

Refer the below JavaScript statements. If there is a statement such as var x; where x is a variable. Then x has a value undefined. The data type is also undefined.

var x;

document. write(x);

document.write(type(x));

This will display the value on the HTML page. It gives undefined. Therefore, it contains a value of undefined. When writing document.write(type(x)); and reloading the page, it will still give undefined. Therefore, the variable x has a value which is undefined and the type is also undefined.

Refer the below statement as well.

var student;

document.write(student);

The variable student has an undefined value. The type of that variable is also undefined.

It is also possible to set the variable value to undefined.  Refer below statement.

var student = undefined;

document.write(student);

document.write(typeof(student));

Now the student variable is having undefined value. The type of variable student is also undefined.

What is the Similarity Between null and undefined?

What is the Difference Between null and undefined?

Null vs Undefined

The null is a data type in JavaScript used to assign a non-value to a variable. The undefined is a data type in JavaScript used when a variable is declared but not assigned with a value.
 Value
When the variable is assigned to null, the value is null. When the variable is assigned undefined, the value is undefined.
Data Type
When the variable is assigned null, the data type is an object. When the variable is assigned undefined, the data type is considered as undefined.

Summary – null vs undefined in JavaScript 

JavaScript is a client-side scripting language used for web application development. It brings interactivity to a web page. It is also used to develop online programs, video games. Along with HTML and CSS, it is a core technology for World Wide Web. Many browsers support JavaScript. When writing programs in JavaScript, it is necessary to store data. Data is stored in variables. Each variable has a data type. Two of them are null and undefined. The  difference between null and undefined in JavaScript is that null is used to assign a non-value to a variable while undefined is used when a variable is declared but not assigned with a value.

Download the PDF of null vs undefined in JavaScript

You can download PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here: Difference Between null and undefined in JavaScript

Reference:

1.tutorialspoint.com. “JavaScript Variables.” The Point, Available here  
2.JavaScript Data Types, w3schools.com Available here  

Image Courtesy:

1.’Javascript badge’By Nikotaf – Own work, (CC BY-SA 4.0) via Commons Wikimedia