Differences between undefined and null in javascript _ javascript skills

Source: Internet
Author: User
There are two primitive types in JavaScript: Null and Undefined. These two types often make JavaScript developers confused, when is it Null, and when is it Undefined? Most computer languages have only one value indicating "None", such as NULL in C, null in Java, None in Python, and nil in Ruby.

It is strange that the JavaScript language has two "NONE" values: undefined and null. Why?

1. Similarity

In JavaScript, assign a variable to undefined or null. To be honest, there is almost no difference.

var a = undefined;var a = null;

In the above Code, variables a are assigned undefined and null, which are almost equivalent.
Both undefined and null are automatically converted to false in the if statement. The Equal operator even directly reports that the two are equal.

if (!undefined)   console.log('undefined is false');// undefined is falseif (!null)   console.log('null is false');// null is falseundefined == null// true

The code above shows how similar the two are!

Since the meanings and usage of undefined and null are similar, why do we need to set two such values at the same time? Doesn't it add JavaScript complexity without reason, and does it bother beginners? Dart, a replacement for JavaScript language developed by Google, makes it clear that there is only null, no undefined!

Ii. Historical Reasons

Recently, when I was reading the new book Speaking JavaScript, I accidentally found the answer to this question!
Originally, this was related to the history of JavaScript. When JavaScript was born in 1995, just like Java, only null was set as the value indicating "NONE.
According to the traditional C language, null is designed to be automatically converted to 0.

Number(null)// 05 + null// 5

But Brendan Eich, the JavaScript designer, thinks this is not enough for two reasons.

First, null is treated as an object like in Java. However, JavaScript data types are divided into two categories: primitive and complex. Brendan Eich thinks that the value indicating "NONE" is better not an object.

Second, the original version of JavaScript does not include an error handling mechanism. When data types do not match, the type is automatically converted or fails silently. Brendan Eich thinks that if null is automatically converted to 0, it is not easy to find errors.

Therefore, Brendan Eich designed an undefined.

Iii. Initial Design

The original version of JavaScript is distinguished as follows: null is an object indicating "NONE", and the value is 0 when it is converted to a value; undefined is an original value indicating "NONE" and NaN when it is converted to a value.

Number(undefined)// NaN5 + undefined// NaN

Iv. Current usage

However, the above distinction will soon prove unfeasible in practice. Currently, null and undefined are basically synonymous, with only slight differences.
Null indicates "no object", that is, there should be no value. Typical usage:
(1) As a function parameter, it indicates that the function parameter is not an object.
(2) serves as the end point of the object prototype chain.

Object.getPrototypeOf(Object.prototype)// null

Undefined indicates "Missing Value", that is, there should be a value, but it is not defined yet. Typical usage:

(1) The variable is declared, but when no value is assigned, it is equal to undefined.
(2) When calling a function, the required parameter is not provided. This parameter is equal to undefined.
(3) The object does not have an attribute assigned a value. The value of this attribute is undefined.
(4) When the function does not return a value, undefined is returned by default.

var i;i // undefinedfunction f(x){console.log(x)}f() // undefinedvar o = new Object();o.p // undefinedvar x = f();x // undefined

Below is a summary

Null is a JavaScript keyword, indicating a non-object.

Undefined indicates no value, indicating that the value does not exist or is not initialized.

Similarities Differences
Unique value of its own type Null is an object and undefined is a reserved word.
No attributes and Methods Null is converted to 0 in mathematical operations. undefined cannot be converted to NaN in mathematical operations.
==Think equal, ===think unequal Null! = Undefined // true
All are dummy values, which indicate the same value in a Boolean variable.

Null is an empty object, and undefined is a window attribute (but not an object attribute)

Alert (typeof null) // object; alert (typeof undefined) // undefined;

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.