DEV Community

Discussion on: JS fundamentals: const vs var vs let?

mse99 profile image
Mohamed Edrah

var still has uses, its particularly useful when you want to declare global variables inside a script, also it could be used to signal that an identifier is function scope and is available throughout the whole function

Also hoisted declarations dont actually get moved, hoisting means that when the function is compiled the instructions to register a variable is placed & executed first, yes javascript is a compiled language not in the same way as something like c or c++ where the code is compiled in advance but instead javascript code is JIT (just in time) compiled the javascript standard doesnt require compilation specifically but it it requires a parse phase.

pentacular profile image

Hoisting does actually move the declaration -- but it does not move the initialization, which becomes an assignment.

So the difference between 'var' and 'let' are two:

  1. var is scoped to the whole function; let is scoped to the whole block.
  2. var is always initialized; let is initialized at the point of declaration, and may not be accessed until initialized.

Javascript is a compiled language

This is a category error.

Javascript implementations may be compilers or interpreters or some mixture of the two.
It is not a property of the language.

mse99 profile image
Mohamed Edrah • Edited on

JS behaves much more like a compiled language than an interpreted one, the code has to be parsed first it then compiled regardless of whether or not the compiled code is machine code or some other byte code format that gets interpreted, the point is JS code gets compiled it's hard to imagine that a production quality is engine parsing the code into an AST and not compile it and optimize it.

Hoisting doesnt move anything, again by move I mean re arranging the code so the declaration is on top of the scope that doesnt actually happen .... when the code gets compiled instructions to register variables are compiled and executed first that it's the engine doesnt rearrange or pre process anything it just turns your code into an efficient executable binary, that's how most JS engine execute code these days and yes JIT compilation IS a property of the language.

Thread Thread
pentacular profile image

"compiled language" and "interpreted language" are nonsensical terms.

There are python compilers, and there are C interpreters.

These are implementation strategies which can be applied to any language.

Hoisting does effectively rearrange the code so that the declarations are at the top of the scope.

See the algorithm in of for example.

JIT compilation is not part of the language specification.

I suggest reviewing the language specification, as it may clarify many of these issues for you.