const vs let when calling require

后端 未结 4 2082
盖世英雄少女心
盖世英雄少女心 2021-02-01 01:08

As io.js now supports ES6 you are finally able to use the const and let keywords. Obviously, let is the successor of var, jus

4条回答
  •  半阙折子戏
    2021-02-01 02:00

    const can be normally used when you don't want your program

    1. to assign anything to the variable

      "use strict";
      const a = 1;
      a = 2;
      

      will produce TypeError: Assignment to constant variable..

    2. to use the variable without explicitly initializing.

      "use strict";
      const a;
      

      will produce SyntaxError: Unexpected token ;

    Simply put, I would say,

    • use const whenever you want some variables not to be modified

    • use let if you want the exact opposite of const

    • use var, if you want to be compatible with ES5 implementations or if you want module/function level scope.

    Use let only when you need block level scoping, otherwise using let or var would not make any difference.

提交回复
热议问题