JavaScript undefined vs. null (translation) __java

Source: Internet
Author: User

Accidentally saw a foreigner wrote the discussion JavaScript in undefined and null article, I also learn to translate a back. Find this English is not to rust.


Original English: http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html

JavaScript undefined vs. null

I was reading a modern, popular book on JavaScript last night and is disappointed by the handling of NULL. The author started out doing a lot to checking like:

Last night, I was reading a JavaScript bestseller, and I was disappointed with the treatment of NULL in the book. The author did a lot of such checks at first:

if (foo = = null) {alert (' foo ' not set. ');}
Then told the reader that they could just remove the = = NULL because JavaScript knows you mean "= null"

Then tell the reader that you can remove "= null" because JavaScript knows that you write "= = null".

what?! This isn ' t why you don ' t check for equality with NULL. It ' s because foo = = null doesn ' t even remotely do what most the IT people in the.

Say what. This is not why you do not check for null. because foo = = NULL is not even indirectly done, in this context many people think it will do (check null).

It ' s a commonly held belief that uninitialized properties in JavaScript are set to NULL as default values. People believe this mostly for 2 reasons:1) foo = = NULL Returns TRUE if Foo is undefined and 2) authors don ' t teach Javas Cript properly.

It is generally assumed that in JavaScript, uninitialized properties are set to NULL by default. This is based on two reasons: 1 if foo is undefined,foo = = NULL Returns TRUE. 2 The author does not teach JavaScript correctly.

A property, where it has no definition, is undefined. Put that way, it ' s pretty obvious.

When an attribute is undefined, it is undefiend. This is a clear case.

Null is an object. It ' s type is null. Undefined is isn't an object, the IT ' s type is undefined. That is less obvious.

Null is an object and its type is null. (Note: This may be a clerical error.) The type of NULL is object. Undefined is not an object and its type is undefined. This part is not very obvious.

The real trouble was that = = does type coercion. = = = Checks for both type and value and are the most intuitive form of equality in JavaScript, in my opinion.

The real problem is that = = do the type conversion. = = = Check both types and values, which I think is the most intuitive form of representation in JavaScript.

I fired up a jash console to hopefully clear things up for you.

I started the Jash console, and I hope I can sort it out for you.

>> Window.hello
Null
>> window.hello.something
Window.hello has no properties
>> Window.hello = null
True
>> Window.hello = = NULL
False
>> Window.hello = = undefined
True
>> if (Window.hello) {alert (' Truthy ');} else {alert (' Falsy ');}/'ll print Falsy.
Null
>> Window.hello = = undefined
True
>> NULL = = undefined
True//There ' s rub, sir.
>> NULL
Null
>> undefined
Null
>> typeof null
Object
>> typeof undefined
Undefined

So people write

if (foo = = null) {
Foo = "Joe";
}
When what they really mean is
if (!foo) {
Foo = "Joe";
}
If you find yourself with a lot of NULL checks in your JavaScript, set aside some time and watch Douglas ' s " JavaScript programming Language "Talk on Yahoo. It ' s Part 1 of a 3-part series of excellent and enlightening talks.

If you find that there are a lot of NULL checks in your JavaScript code, take a moment to look at Douglas Crockford's speech on the JavaScript programming language in Yahoo Video. It is the first part of a series of inspiring and excellent speeches.

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.