Home >Web Front-end >Front-end Q&A >Is it possible to use the same variable name in different functions in Javascript?

Is it possible to use the same variable name in different functions in Javascript?

烟雨青岚
烟雨青岚Original
2020-06-22 16:51:535526browse

Is it possible to use the same variable name in different functions in Javascript?

#Can I use the same variable name in different functions in Javascript?

The same variable name can be defined in different functions in JavaScript. Because it is defined inside the function body, it is a local variable and cannot be accessed by the outside world. . So it’s ok.

Expand knowledge:

The difference between global variables and local variables is as follows:

1. Different scopes: The scope of global variables is the entire program, while the role of local variables The domain is the current function or loop, etc.

2. The memory storage methods are different: global variables are stored in the global data area, and local variables are stored in the stack area

3. The life cycle is different: the global variables The life cycle is the same as that of the main program. It is destroyed when the program is destroyed. Local variables are inside the function or inside the loop and disappear when the function exits or the loop exits.

4. The usage method is different: global variables are declared in Afterwards, all parts of the program can be used, but local variables can only be used locally. Within the function, local variables are used first before global variables.

Local variables can have the same name as global variables, but local variables will shield global variables.

Global variables are a type of programming terminology, derived from variables.

Variables are divided into local and global variables. Local variables can also be called internal variables. Variables created by an object or a function are usually local variables and can only be referenced internally and cannot be referenced by other objects or functions.

Global variables can be created by an object function or anywhere in the program. Global variables can be referenced by all objects or functions in this program.

Recommended tutorial: "JS Tutorial"

The above is the detailed content of Is it possible to use the same variable name in different functions in Javascript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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