Do you really know about null and undefined in JavaScript?

Source: Internet
Author: User

I have been learning JavaScript during this time and encountered such a problem while learning arrays.

A piece of code is provided:


Analysis on the book:
However, during the test, I found that week [0] ~ Week [3] does not seem to be initialized as null. To verify my conjecture, I made the following example:
<HTML> The result is displayed as follows:

If we assign a value to week [4] as described in the book, week [0] ~ will be automatically assigned Week [3] is initialized to null. If week [0] is null, the returned results should be the same as week [6]. The value is null and the type is object, but the result is not consistent with the expectation! In the test, if week [0] is null or undefined, true is returned, which is confusing.
After studying for a long time, suddenly the null and undefined values were very vague, so I quickly checked the information. Finally, I found several confusing points:
Null: indicates no value; undefined: indicates an undeclared variable, a declared variable without a value assignment, or an object attribute that does not exist.
According to this point of view, the following tests are performed on null and undefined:
        document.write(typeof(null)+"<br>");        document.write(typeof(undefined)+"<br>");         document.write((null == undefined)+"<br>");

Output three values: Object undefined true. Further research finds that null is an object, so when the return type is reached, the object will be returned. Null is the reserved keyword of JavaScript. The value of null is automatically converted to 0 when involved in the numeric operation. Therefore, the following expression is calculated to obtain the correct value: expression: 123 + null result value: 123 expression: 123 * null result value: 0
Undefined is a special attribute of the Global Object (window), and its value is undefined. However, typeof undefined returns 'undefined '. You can find the explanation by querying "undefined attributes" in the JScript manual. We can use the following example to verify whether undefined is a window attribute.

Alert ('undefined' in window); // output: True var anobj ={}; alert ('undefined' in anobj); // output: false

Although undefined is an attribute with special meanings, it is not a reserved keyword of JavaScript.
So what is the relationship between null and undefined. It can be said that if the undefined value of a variable is true, it is also set to true when compared with null. As long as the variable is not initialized, its undefined must be true. Therefore, the value of undefined and null is false only when the variable is initialized. Let's look at the example below:
VaR A = 123; document. write ("the value of A is:" + A + "<br>"); document. write ("a Type:" + typeof (A) + "<br>"); document. write ("whether a is null:" + (A = NULL) + "<br>"); document. write ("whether a is undefined:" + (A = undefined) + "<br>"); var B = NULL; document. write ("B value:" + B + "<br>"); document. write ("B type:" + typeof (B) + "<br>"); document. write ("Whether B is null:" + (B = NULL) + "</BR>"); document. write ("Whether B is undefined:" + (B = undefined) + "<br> ");

The result is: Do you feel a bit wrong when you see this result? Do you think "B is not of the undefined type, but an object? It indicates that it has been initialized. Follow the undefined explanation above. At this time, the undefined of B and the comparison with null should be false "? Don't worry. Listen to me. I started with this, but I thought about it carefully and found a problem. First, the running result is certainly correct, and the undefined explanation above is also correct. So why is B of the object type? Var
B = NULL first, it can be seen in the form that it is an initialization process, but because the initialization is null, the initialization becomes less authentic. Initialization is divided into two steps: ① allocating space and ② assigning values. VaR B = NULL completes the space allocation step, but the value is null when the value is assigned, which is equivalent to no value. So strictly speaking, the "procedure" for normal Initialization is not completed, so its undefined and comparison with null are still true. You can add this sentence after var A = 123; A = NULL; after running, you will find that the result is exactly the same as that displayed by B. This indicates that if a variable is null, undefined is true. So in a sense, null = undefined. In conclusion, we can infer that week [0] In the first example is only a declared but not assigned value variable, so undefined is true, and then null is true. But it is not initialized to null. If the initialization is null, its value must be null. Therefore, I am wrong about my opinion in the book.
Therefore, it is unscientific to use week [0] = NULL to judge the value of week [0], because week [0] is not defined or is not initialized, it is compared with null, the result is true. If the value is null after initialization, the result is compared with null, which is exactly the same. Therefore, true is returned.

I wrote this blog and used it to help you answer your questions. I just suddenly understood it. At the beginning, I found a lot of materials, but I still didn't understand them. But I wrote it, and suddenly a flash of wisdom opened up. However, this is just my ignorance. It may not be correct, if you find any errors, please correct me in time.

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.