Comparison of undefined and null examples in JS

Source: Internet
Author: User
Tags comparison error handling

Oddly enough, the JavaScript language actually has two values that represent "none": Undefined and null. Why is that?

First, similarity

In JavaScript, assigning a variable to undefined or null is, to be honest, almost indistinguishable.

The code is as follows Copy Code

var a = undefined;
var a = null;

In the code above, the A variable is assigned to undefined and null, and the two are almost equal to each other.

Undefined and null are automatically converted to false in an if statement, and the equality operator even reports directly to the same.

The code is as follows Copy Code

if (!undefined)
Console.log (' undefined is false ');
Undefined is false
if (!null)
Console.log (' null is false ');
Null is False
undefined = null
True

The code above shows how similar the behavior is!

Since undefined and null are similar in meaning to usage, why should you set two such values at the same time, this is not an unwarranted increase in the complexity of JavaScript, so that beginners bothered? Google has developed a JavaScript language alternative to the Dart language, which explicitly stipulates that only null, no undefined!
second, historical reasons

I recently discovered the answer to this question when I was reading the book Speaking JavaScript!

Originally, this is related to the history of JavaScript. When JavaScript was born in 1995, it was originally like Java, setting only null as a value that represented "none."

According to the C language tradition, NULL is designed to be automatically converted to 0.

The code is as follows Copy Code

Number (NULL)
0
5 + NULL
5

But Brendan Eich, a JavaScript designer, feels it's not enough, for two reasons.

First, NULL is treated as an object, as in Java. However, the data type of JavaScript is divided into two categories, the original type (primitive) and the composite type (complex), Brendan Eich feel that the value representing "None" is best not an object.

Second, the original version of JavaScript did not include the error handling mechanism, when the data type mismatch occurred, often the automatic conversion type or silently failed. Brendan eich that if NULL automatically to 0, it is not easy to find errors.

Therefore, Brendan Eich also designed a undefined.
third, the original design

The original version of JavaScript is distinguished by this: null is an object that represents "none", and when converted to a value, 0;undefined is an original value representing "None", which is Nan when converted to a value.

The code is as follows Copy Code

Number (undefined)
NaN
5 + undefined
NaN

Iv. Current Usage

However, the above distinction is soon proved to be not practical in practice. At present, null and undefined are basically synonymous, with only a few subtle differences.

Null means "No object", where there should be no value. Typical uses are:
(1) as a parameter of the function, which indicates that the parameter of the function is not an object.

(2) As the end point of the object prototype chain.

The code is as follows Copy Code

Object.getprototypeof (Object.prototype)
Null

Undefined means "missing value," where there should be a value, but not yet defined. Typical uses are:
(1) When a variable is declared, it is equal to undefined when it is not assigned a value.

(2) when calling a function, the supplied argument is not supplied, which equals undefined.

(3) The object has no assigned property, and the value of the property is undefined.

(4) When the function does not return a value, the default returns undefined.

The code is as follows Copy Code

var i;
I//undefined
function f (x) {Console.log (x)}
F ()//undefined
var o = new Object ();
O.P//undefined
var x = f ();
X//undefined

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.