Difference between let and var definition of variables in js

javascript strict mode

The first time I came across the let keyword, there was a toBe very, very careful.is the concept of "javascript strict mode", for example, the following code will run with an error:

let hello = 'hello world.
console.log(hello);

The error message is as follows:

let hello = 'hello world.';
^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    ...

The solution is to add a "javascript strict mode" declaration to the header of the file:

'use strict';

let hello = 'hello world.
console.log(hello);

For a more detailed description of "javascript strict mode", please refer to Ruan Yifeng's blog.
Javascript Strict Patterns Explained

Similarities and differences between the let and var keywords

Declared without assignment, behaves the same

'use strict';

(function() {
  var varTest; let letTest; var varTest
  var varTest; let letTest; console.log(varTest); // output undefined
  console.log(varTest); // output undefined
  console.log(letTest); // output undefined
}()).

Using undeclared variables behaves differently: the

(function() {
  console.log(varTest); // output undefined (note that you have to comment out the following line to run it)
  console.log(letTest); // direct error: ReferenceError: letTest is not defined

  var varTest = 'test var OK.
  let letTest = 'test let OK.'; }()); // Direct error: ReferenceError: letTest is not defined.
}());

behaves differently when the same variable is declared repeatedly:

'use strict';

(function() {
  var varTest = 'test var OK.'; let let letTest = 'test let OK.'; var varTest = 'test var OK.
  let letTest = 'test let OK.'; (function( { var varTest = 'test var OK.'; let letTest = 'test let OK.

  var varTest = 'varTest changed.'; / let letTest = 'letTest changed.
  let letTest = 'letTest changed.'; // direct error: SyntaxError: Identifier 'letTest' has already been declared

  console.log(varTest); //output varTest changed. (Note that you have to comment out the duplicate declarations of the letTest variable above to run it)
  console.log(letTest); // output varTest changed.
}()).

Scope of action of variables, different performance

'use strict';

(function() {
  var varTest = 'test var OK.'; let let letTest = 'test let OK.'; var varTest = 'test var OK.
  let letTest = 'test let OK.'; (function( { var varTest = 'test var OK.'; let letTest = 'test let OK.

  {
    var varTest = 'test var OK.'; let letTest = 'test let OK.'; { var varTest = 'varTest changed.
    let letTest = 'letTest changed.'; }
  }

  console.log(varTest); // output "varTest changed.", the varTest variable declared in the internal "{}" overrides the external letTest declaration
  console.log(letTest); // output "test let OK.", the variable letTest declared in the internal "{}" is not the same as the external letTest.
}()).
912sy.com download resources are from the network, only for learning and reference use, the copyright belongs to the original author, do not use for commercial purposes, please remove yourself within 24 hours after downloading.
If the content published on this site inadvertently violates your rights and interests, please contact us and the site will be deleted within one business day.If you encounter any problems please contact customer service QQ:2385367137
912sy " Difference between let and var definition of variables in js