Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Local variables: variables defined within a function are called local variables.

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

The difference between local and global variables:

1: scope is different

The scope of a local variable is within the current function.

The scope of the global variable is within the entire script tag. Including within the function.

2: the life cycle is different

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

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

3: different locations are defined

The local definition is in the body of the function.

The global definition is outside the function.

4: the priority of being accessed is different.

If local variables and global variables have duplicate names, then local variables within the function are accessed at a higher priority. It can be done through window. To access the global variable, or this.

5: different memory

Locally in the stack frame, the stack frame is in the stack.

The global belongs to the properties of the window object, and the window object is in heap memory; so the global is in the heap.

6: the speed of access is different

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

Thank you for your reading, these are the contents of "what are the differences between global variables and local variables in javascript". After the study of this article, I believe you have a deeper understanding of the difference between global variables and local variables in javascript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report