Why is var not deprecated?

前端 未结 3 1632
难免孤独
难免孤独 2020-12-29 08:47

Lately after ES6 released, many sources suggested that I use \"const\" and \"let\" instead of \"var\", and that I should stop using \"var\" in my JavaScript.

What I

3条回答
  •  Happy的楠姐
    2020-12-29 09:20

    Everything has their own advantages and disadvantages using var const and let is dependent on their use cases.

    var
    

    Variable declarations are processed before the execution of the code. The scope of a JavaScript variable declared with var is its current execution context. The scope of a JavaScript variable declared outside the function is global.

    let
    

    The let statement allows you to create a variable with the scope limited to the block on which it is used.

    const
    

    const statement values can be assigned once and they cannot be reassigned. The scope of const statement works similar to let statements.

    I hope you understand.

提交回复
热议问题