Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming /Difference Between null and undefined in JavaScript

Difference Between null and undefined in JavaScript

January 30, 2018Posted byLithmee

Key Difference – nullvs undefined in JavaScript

JavaScriptis 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. Thekey differencebetween null and undefined in JavaScript is thatnull 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 supportsObject Oriented Programming. A program or the software can be modeled intoobjectsusing 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};

Difference Between null and undefined in JavaScript

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?

  • Both are data types in JavaScript.

零和未定义的区别是什么?

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 – nullvs 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.comAvailable here

Image Courtesy:

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

Related posts:

Difference Between Flowchart and Data Flow Diagram (DFD) Difference Between Prolog and Lisp Difference Between Inheritance and Composition Difference Between getc and getchar_Figure 02Difference Between getc and getchar Difference Between High Level Language and Low Level LanguageDifference Between High Level Language and Low Level Language

Filed Under:ProgrammingTagged With:Compare null and undefined in JavaScript,null and undefined in JavaScript Differences,null and undefined in JavaScript Similarities,null in JavaScript,null in JavaScript Data Type,null in JavaScript Definition,null in JavaScript Value,null vs undefined in JavaScript,undefined in JavaScript,undefined in JavaScript Data Type,undefined in JavaScript Definition,undefined in JavaScript Value

About the Author:Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Android 4G Samsung Infuse 4G and Motorola Atrix 4G

Difference Between Android 4G Samsung Infuse 4G and Motorola Atrix 4G

Difference Between Basel 1 2 and 3

Difference Between Basel 1 2 and 3

Difference Between Academy Awards and Oscars

发音和发音的区别

发音和发音的区别

Difference Between Interspecific and Intraspecific Competition

Difference Between Interspecific and Intraspecific Competition

Latest Posts

  • What is the Difference Between Tylosin Tartrate and Tylosin Phosphate
  • What is the Difference Between Sarcoptic and Demodectic Mange
  • What is the Difference Between Methyl Orange and Phenolphthalein
  • What is the Difference Between Melasma and Chloasma
  • What is the Difference Between Thyme Linalool and Thyme Thymol
  • What is the Difference Between MCT and LCT
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.