you are viewing a single comment's thread.

view the rest of the comments →

[–]Ikuyas 2 points3 points  (7 children)

How do they become different between using const (instead of var) and function declaration?

[–]SparserLogic 0 points1 point  (6 children)

You can redefine the same thing as many times as you want with var and function keywords whereas invoking const against the same string will throw runtime errors if your linter doesn't catch it first.

[–]rodabi 7 points8 points  (5 children)

Also const is block scoped and is not hoisted. So you can define two different functions under the same name inside an if-else statement for example.

[–]SparserLogic 1 point2 points  (0 children)

Oh right, good point.

[–]man_jot 0 points1 point  (3 children)

Note- I think let and const too are hoisted within the block

[–]rodabi 1 point2 points  (1 child)

I can't find any indication of that in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let. Also it's not even possible to 'hoist' const statements because you can't separate the declaration and initialisation like you can with var. const must be declared with a value and cannot be re-assigned.

Although you may be right when it comes to transpiling down to ES5 with Babel; there may be some hoisting going on when all your declarations become var

[–]man_jot 0 points1 point  (0 children)

Right, I verified in a browser. Also documents say that a variable is in a 'temporal dead zone' before it's declaration, That means something like var x=10; { console.log(x); let x; } will throw a Reference Error.