What is the difference between global variables and local variables in javascript
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!