Easy to understand the difference between var, let, const

ECMAScript 6 (referred to as ES6) is the next generation standard JavaScript language, in June 2015 officially released, also known as ECMAScript 2015.

Benefits for ES6

ES6 appearance has brought great convenience to our front-end, a feature previously only achieved by a few dozen lines of js, ES6 a few simple lines of code will be able to achieve, ES6 some of the new features, changes at many of the drawbacks, For example let emerging, const, to achieve a function block-level scope reduced var previous global variables, function instead of lexical scope of arrow "this", etc., where for example more sub do not want For more, please refer https://www.w3cschool.cn/ecmascript/pgms1q5d.html , which today together with you to find out the difference between the var, let, const

let

let ES6 new command is used to declare variables like var to declare variables. But Talia is quite different.

  • Variable let, const declared valid only within the block-level action, var declared variables are global, no function block-level scope
  • let, const variable lift absent, var presence of variable
  • let, const can not be repeated in the same block application-level scope

Definition of variables using let run the following code:

    const arr = [1, 2, 3, 4]
    for (let i = 0; i < arr.length; i++) {
    console.log(arr[i])
    }
        console.log(i);

Here Insert Picture Description
Given i defined above, define the variable i let described, act only in the block where the function domain

var defined variable operation code as follows:

var arr = [1, 2, 3, 4]
    for (var i = 0; i < arr.length; i++) {
            console.log(arr[i])
        }
        console.log(i);

Here Insert Picture Description
Operating results described above, the variable is not defined var block-level scope, I can still output in the block-level scope
fact var named variable in the function is a function only work in the whole scope, a scope can not function used, for example, the following

    function testVar () {
            var i = 2
            console.log('函数内:' + i)
        }
        testVar()
        console.log('函数外:' + i)

Here Insert Picture Description
Obviously output outside a function of the variable i when i reported undefined, var named variable in the function is a function only work in the whole scope

let unlike var as "variable lift" phenomenon occurs. Therefore, the variable must be declared after use, otherwise an error.
Run the following code:

    console.log(arg1)
    console.log(arg2)
    var arg1 = 'test'
    let arg2 = 'test2'

Here Insert Picture Description
Arg1 variable declared with the var command, variable lift will occur, that the script starts running, the variable arg1 already exists, but no value, so the output will be undefined. Arg2 variable with the let command statement, the variable will not happen upgrade. This means that before declaring it, arg2 variable does not exist, then if it is used, it will throw an error.

var run repeatedly affirmed the results of variables:

    var arg1 = 'test'
        console.log('var第一次申明:' + arg1)
        var arg1 = 'test2'
        console.log('var第二次申明:' + arg1)

Here Insert Picture Description
let Repeat the code to declare variables

let  arg2 = 'test'
console.log('let第一次申明:' + arg2)
let  arg2 = 'test2'
console.log('let第二次申明:' + arg2)

Here Insert Picture Description
let declare variables can not be repeated in the same block scope, var statement can be repeated

const

const and let the above characteristics the same, but there are differences, let statement is a variable, const declaration is constant, read-only, modify the value will complain, const save that memory address, you can add attributes or elements to objects or arrays, but can not re-replication.
To learn more, please scan two-dimensional code
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/lfcss/p/12167190.html