Home > Web Front-end > Front-end Q&A > What is the meaning of keyword let in jquery

What is the meaning of keyword let in jquery

青灯夜游
Release: 2022-07-28 16:18:02
Original
2676 people have browsed it

In jquery, let is a keyword for declaring variables. The let keyword allows you to declare a variable, statement or expression whose scope is limited to the block scope. Variables declared by let will not be promoted in the scope, it is initialized at compile time; let will not create properties of the window object when declared globally (in the top-level scope).

What is the meaning of keyword let in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.0 version, Dell G3 computer.

In jquery, let is a keyword that declares variables.

The let keyword declares a block-scoped local variable and can be initialized to a value (optional). Syntax:

let name1 [= value1] [, name2 [= value2]] [, ..., nameN [= valueN];
Copy after login

Parameters

  • nameN: The name of one or more variables to be declared, must be a legal identifier.

  • valueN: Optional, specifies the initial value of the variable, which can be any legal expression.

Example:

let x = 1;

if (x === 1) {
  let x = 2;

  console.log(x);
  // expected output: 2
}

console.log(x);
// expected output: 1
Copy after login

What is the meaning of keyword let in jquery

Description:

let allows you to declare that a scope is restricted A variable, statement, or expression in block scope. Different from the var keyword, the variable scope declared by var is global or the entire function block. Another important difference between var and let is that the variable declared by let will not be promoted in the scope, it is initialized at compile time (see the temporary dead zone below).

Just like const, let will not create properties of the window object when declared globally (in the top-level scope).

You can learn why we use let from here.

You can avoid many problems by declaring let variables at the top of the scope where they are used, but doing so may affect readability.

Different from var, let is just the beginning of a statement, not a complete expression. This means that you cannot have a separate let declaration as the body of a block of code (which makes sense, since declared variables cannot be accessed).

if (true) let a = 1; // SyntaxError: Lexical declaration cannot appear in a single-statement context
Copy after login

Scope rules

The variable scope declared by let is only within the block or sub-block in which it is declared. This is similar to var. The main difference between the two is that the scope of the variable declared with var is the entire enclosing function.

function varTest() {
  var x = 1;
  {
    var x = 2; // same variable!
    console.log(x); // 2
  }
  console.log(x); // 2}function letTest() {
  let x = 1;
  {
    let x = 2; // different variable
    console.log(x); // 2
  }
  console.log(x); // 1}
  
  
letTest();
varTest();
Copy after login

What is the meaning of keyword let in jquery

In the global scope, let is different from var in that it does not create properties on the global object. For example:

var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined
Copy after login

What is the meaning of keyword let in jquery

Duplicate declaration

Repeated declaration of the same variable in the same function or block scope will throw a SyntaxError .

if (x) {
  let foo;
  let foo; // SyntaxError thrown.
}
Copy after login

This error will also be triggered in the switch statement because it is the same block scope.

let x = 1;

switch(x) {
  case 0:
    let foo;
    break;
  case 1:
    let foo; // SyntaxError for redeclaration.
    break;
}
Copy after login

What is the meaning of keyword let in jquery

However, it is important to note that a block nested in a case clause will create a new block-scoped lexical environment and will not generate Appeal repeats statement of error.

let x = 1;

switch(x) {
  case 0: {
    let foo;
    break;
  }
  case 1: {
    let foo;
    break;
  }
}
Copy after login

【Recommended learning: jQuery video tutorial, web front-end video

The above is the detailed content of What is the meaning of keyword let in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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