Introduction to JS variables and their scope knowledge points

巴扎黑
Release: 2017-08-17 14:00:21
Original
1416 people have browsed it
1. Variables and their scope: Variables are divided into "global variables" and "local variables". "Global variables" are declared outside the function and can be used by all functions, while "local variables" are declared inside the function body and can only be used in Used within the function body in which the variable is defined.
(Note: There is no var keyword when declaring variables, but directly assigned variables are global variables)
Copy after login

2. In the function body, local variables have higher priority than global variables.
Copy after login

According to my understanding, the final output answer of the above example should be: 1 2 2 1; but the correct answer is: undefined 2 2 1; The reason is that the same name is declared both outside and inside the function body variables, local variables override global variables.
3. How to read the local variables inside the function body from the outside?
Generally speaking, only external global variables can be directly obtained from inside the function body, but local variables inside the function body cannot be obtained from the outside. However, this can be achieved by defining another function inside the function body to return local variables, and then calling the function from the outside.
Copy after login

The above is the detailed content of Introduction to JS variables and their scope knowledge points. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!