What is the difference between undefined and undeclared in JavaScript?

Source: Internet
Author: User

I think the fundamental difference is that undefined is a JS language type, while undeclared is a JS syntax error.
--------------------------------- Answer part 1 ---------------------------------------
In JS, there are two undefined and null values for 'null', among which undefined is useful. Undefined is an undefined type. The javascript language also defines a global variable whose value is undefined, which is also called undefined. However, this variable is neither a constant nor a keyword. This means that its value can be easily overwritten. To avoid possible changes to the undefined value, a common technique is to use an additional parameter passed to the anonymous package. During the call, this parameter does not get any value. Example:
VaR undefined = 123;
(Function (something, Foo, undefined ){
// The undefined variable in the local scope gets the 'undefined' value again.
}) ('Hello world', 42 );
A better example is the early jquery Code (In jquery before version 1.5), you will find that the first line is called directly by function (window, document, undefined. Many people do not understand the usage of the undefined variable. In fact, the undefined variable here is like this.
For this part of the answer, refer to self-JavaScript Garden: bonsaiden.github.com/javascri... (well-written JS quirks tutorial, I suggest you go and have a look !)
---------------------------- Answer part 2 ---------------------------------
Undeclared is a syntax error. @ sun Datong's answer is that accessing the undeclared variable does not interrupt browser execution. In the context of running the browser, the undeclared variable can be considered as not defining the variable like var A. During execution of the JS engine, the corresponding context (scope) cannot be found ), it will simply think that the variable is a global variable, that is, it will be defined in the window!
Many of you are writing for (VAR I = 0, L = xx. if you forget to write var statements such as length; I <L; I ++ ......), therefore, the variables I and l are directly changed to global variables, and then an error occurs. However, despite the error, the variables I and l are accessible ~~~
Refer to @ zhiyelee's updates and add it to this answer: note that the variable does not use VaR display declaration, but has been assigned a value before use, this value is implicitly declared as a non-global variable.
Even code like B = "null" can be accessed using window. B without interrupting the browser!
Specific evidence in Mozilla's MDC Community For more information about the variable undeclared caused by the VaR keyword, see the link 2.16.mozilla.org/en/javasc.
------------------------------ Comprehensive installation force ---------------------------------
To sum up, the dynamic running of JS is the root cause of the above problem. The JS parser requires the keyword VaR to determine the scope of the variable, and the context of the variable, I personally think, almost all the essence of JS! What is this keyword? The context of the closure is only available for determination!

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.