Javascript causes undefined and varundefined by using var incorrectly.

Source: Internet
Author: User

Javascript causes undefined and varundefined by using var incorrectly.

In javascript, variables are divided into local variables and global variables based on different scopes. The variables directly defined are global variables, which can be accessed by all scripts; the variables defined in the function are local variables, which are only valid in the function.
If the global variable and local variable use the same name, the local variable overwrites the global variable.
Sample Code:

<! DOCTYPE html> 

The running result is as follows:
Click test1 to bring up the local variable.
Click test2, undefined is displayed, and local variables are displayed.
 
Click test3 to bring up global variables and then local variables.

This is the difference between using var and not using it:
If var is used,The program will force a new variable to be defined.
If var is not used,The system will first search for the existence of the variable in the current context. A new variable will be redefined only if it does not exist.

In test3, all variables a are global variables. The first time the global variables are directly output, and the second time the global variables are directly assigned values.
Both test1 and test2 use var in the function to define a new variable with the same name. This will overwrite the global variable in the function. Therefore, in test2: the first output of a is the local variable a that has been overwritten, but the initial value is not given.UndefinedThis result. Using variables in this way is actually incorrect and should be avoided.

Articles you may be interested in:
  • ''Var ''In JScript defines the scope of the variable.
  • Implementation Code of the var_dump function in Javascript
  • JavaScript code used to determine undefined
  • Why do I have to add var to define the I variable in the js for loop?
  • Inference on the scope of variables declared by var in JavaScript
  • Detailed analysis of differences between js and undefined types, undefined and null
  • Differences between undefined and not defined in javascript
  • How to judge null, undefined, and NaN in JS
  • How to judge undefined in JS (a good method)
  • Differences between var and var when defining variables in javascript

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.