The difference between let and Var defined variables in JS

Source: Internet
Author: User

JavaScript Strict mode

The first contact with the Let keyword, there is a very very important concept is "JavaScript strict mode", such as the following code to run the error:

let hello = ‘hello world.‘;console.log(hello);
    • 1
    • 2
    • 1
    • 2

The error message is as follows:

‘hello world.‘;^^^SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode    ...
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

The workaround is to add a "JavaScript strict mode" declaration to the file header:

‘use strict‘;let hello = ‘hello world.‘;console.log(hello);
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

For more detailed instructions on "JavaScript strict mode", please refer to Nanyi's blog
A detailed description of Javascript in strict mode

Let and VAR keywords are not assigned after the declaration of similarities and differences, the same performance
‘use strict‘;(function() {  var varTest; let letTest; console.log(varTest); //输出undefined console.log(letTest); //输出undefined}());
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
Use undeclared variables to behave differently:
(function() {  console.log(varTest); //输出undefined(注意要注释掉下面一行才能运行) console.log(letTest); //直接报错:ReferenceError: letTest is not defined var varTest = ‘test var OK.‘; let letTest = ‘test let OK.‘;}());
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
When declaring the same variable repeatedly, it behaves differently:
‘use strict‘;(function() {  var varTest = ‘test var OK.‘; let letTest = ‘test let OK.‘; var varTest = ‘varTest changed.‘; let letTest = ‘letTest changed.‘; //直接报错:SyntaxError: Identifier ‘letTest‘ has already been declared console.log(varTest); //输出varTest changed.(注意要注释掉上面letTest变量的重复声明才能运行) console.log(letTest);}());
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
Variable action range, different performance
‘use strict‘;(function() {  var varTest = ‘test var OK.‘; let letTest = ‘test let OK.‘; { var varTest = ‘varTest changed.‘; let letTest = ‘letTest changed.‘; } console.log(varTest); //输出"varTest changed.",内部"{}"中声明的varTest变量覆盖外部的letTest声明 console.log(letTest); //输出"test let OK.",内部"{}"中声明的letTest和外部的letTest不是同一个变量}());

The difference between let and Var defined variables in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.