A brief discussion on JavaScript scope

A brief discussion on JavaScript scope

1. Scope

Generally speaking, the names used in a program code are not always valid and available, and the scope of code that limits the availability of the name is the scope of the name. The use of scope improves the locality of program logic, enhances program reliability, and reduces name conflicts.

There are two types of scopes in JavaScript (before es6):

  • Global Scope
  • Local scope (function scope)
  • After ES6, there is also a block-level scope, which will be described in detail later.

1. Global scope

Applies to the environment where all code is executed (the entire script tag) or a separate js file.

2. Local scope

The code environment that acts on a function is the local scope. Because it is related to functions, it is also called function scope.

For example:

  for(let i=0;i<100;i++){
       sum += i;
   }

2. Scope of variables

In JavaScript, variables can be divided into two types according to their scope:

  • Global variables
  • Local variables

1. Global variables

Variables declared in the global scope are called global variables (variables defined outside a function).
Global variables can be used anywhere in the code. Variables declared with var in the global scope are global variables. In special cases, variables declared without var in a function are also global variables (not recommended).

2. Local variables

Variables declared in a local scope are called local variables (variables defined inside a function)
Local variables can only be used within a function. Variables declared with var within a function are local variables. Function parameters are actually local variables.

3. The difference between global variables and local variables

  • Global variables: can be used anywhere and will only be destroyed when the browser is closed, so they take up more memory.
  • Local variables: are only used inside a function. They are initialized when the code block they are in is executed. They are destroyed when the code block is finished, thus saving more memory space.

3. Scope Chain

According to the mechanism that inner functions can access outer function variables, chain search is used to determine which data can be accessed by inner functions, which is called scope chain.

  • As long as it is code, it has at least one scope
  • Local scope written inside a function
  • If there is a function within a function, then another scope can be created within this scope.

For example: Analyze the following code to determine the result.

function f1() {
    var num = 123;
    function f2() {
        console.log( num );
    }
    f2();
}
var num = 456;
f1();

The analysis is shown in the figure below:

It can be seen that the final result is: 123

Similarly, the final value of the variable can also be found by adopting the proximity principle.

This is the end of this article about the details of JavaScript scope. For more relevant JavaScript scope content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript ES new feature block scope
  • JavaScript Basics: Scope
  • JavaScript Advanced Programming: Variables and Scope
  • Graphical explanation of the underlying principle of JavaScript scope chain
  • JavaScript static scope and dynamic scope explained with examples
  • Javascript scope and closure details
  • JS Difficulties Synchronous and Asynchronous and Scope and Closure and Detailed Explanation of Prototype and Prototype Chain

<<:  HTML end tag issue and w3c standard

>>:  Solution to abnormal connection table caused by inconsistent MySQL character sets

Recommend

A small introduction to the use of position in HTML

I just learned some html yesterday, and I couldn&#...

Node.js+postman to simulate HTTP server and client interaction

Table of contents 1. Node builds HTTP server 2. H...

How to use Samba to build a shared file service on a Linux server

Recently, our small team needs to share a shared ...

JavaScript code to implement a simple calculator

This article example shares the specific code of ...

Analysis of a MySQL deadlock scenario example

Preface Recently I encountered a deadlock problem...

JavaScript implements displaying a drop-down box when the mouse passes over it

This article shares the specific code of JavaScri...

Implementation steps of Mysql merge results and horizontal splicing fields

Preface Recently, I was working on a report funct...

Front-end advanced teaching you to use javascript storage function

Table of contents Preface Background Implementati...

Vue routing lazy loading details

Table of contents 1. What is lazy loading of rout...

Comparison of storage engines supported by MySQL database

Table of contents Storage Engine Storage engines ...

DOM operation implementation in react

Table of contents Previous words Usage scenarios ...

Conflict resolution when marquee and flash coexist in a page

The main symptom of the conflict is that the FLASH...

Detailed explanation of Mencached cache configuration based on Nginx

Introduction Memcached is a distributed caching s...