Home > Web Front-end > Front-end Q&A > What is the difference between global variables and local variables in javascript

What is the difference between global variables and local variables in javascript

青灯夜游
Release: 2022-02-15 17:02:39
Original
17164 people have browsed it

Difference: 1. The life cycle of global variables depends on the creation and destruction of the window, while the life cycle of local variables depends on the calling cycle of the function, from the beginning to the end of the call; 2. Global variables are defined in the function In addition, local variables are defined in the function body; 3. The memory locations are different. Global variables are in the heap, while local variables are in the stack.

What is the difference between global variables and local variables in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Local variables: Variables defined inside a function are called local variables.

Global variables: Variables defined outside the function are global variables.

The difference between local variables and global variables:

1: Different scopes

The scope of local variables is within the current function.

The scope of global variables is within the entire script tag. Included within functions.

2: Different life cycles

The life cycle of local variables depends on the calling cycle of the function, from the beginning to the end of the call.

The life cycle of global variables depends on the creation and destruction of the window.

3: The location of the definition is different

The local definition is in the function body.

The global definition is outside the function.

4: The accessed priorities are different.

If local variables and global variables have the same name, then the local variable has a higher priority when being accessed within the function. Global variables can be accessed through window., or this.

5: The memory locations are different.

The local memory is in the stack frame, and the stack frame is in the stack.

Global properties belong to the window object, and the window object is in the heap memory; so the global properties are in the heap.

6: The access speed is different

The access efficiency of local variables is higher than that of global variables.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What is the difference between global variables and local variables in javascript. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template