Why does JavaScript have to be null? Translation

Source: Internet
Author: User

Original address

JavaScript has a lot of quirks and incomprehensible places. Among them null& undefined is more interesting. Now that you have it, why does JavaScript have to get a null?

Equality Comparison

Let's start by having a look at the following comparison to explore null and undefined:

NULL //   NULL//NULL//NULL//   null//false

How can null be greater than or equal to zero, and less than or equal to zero, but not equal to zero? (is not the beginning of suspicion of computer, is not very rigorous it)

To understand this, you first need to know how JavaScript handles these comparisons. In the example above, two different types of operators are used: the equality operator [Equality Operators] (==,===,! = and! = = =) and relational operators [relational Operators] (>,<,> = and <=). The two work in different ways.

The equality operator is compared at the object level. If the two operands, the things around the operator are different types, use a strict comparison (= = = or!). = =). Because null is an object, 0 is a number, and they are not equal.

In the case of a relational operator, two operands are converted to the same type. Give a number in the example. In the current scenario, JavaScript is performing the following actions:

Number (null//true

Explain further. Number (NULL) returns the value 0, and then 0 equals 0.

Let's try the same example with undefined :

Undefined >= 0; // <= 0;  // = = 0;  // < 0;  // > 0;  // false

The first instinct is that this should be the same result. Although JavaScript is the same, the result is again different. JavaScript first converts undefined to a number, but numbers (undefined ) is not 0 but NaN. It is clear in the JavaScript specification that each comparison with Nan will be false, even compared to nan .

Of course, there may be many differences in the algorithm, and the current explanation can explain some problems.

why is null present?

Does JavaScript need a null value? Although you can write an entire application without using NULL, it usually has a null place. The difference of use is intention. A null value is very neutral, and one of the undefined values is usually unintentional, just the default value.

For example, in a function that retrieves a single piece of data. This function returns NULL when the data does not exist. Otherwise, the data is returned. In this way, you can determine whether the value returned by the function is set to NULL, or the variable that contains the result is not set at all.

In other words: null!== undefined

Why does JavaScript have to be null? Translation

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.