I was wondering what states are kept between two lines of JavaScript code submitted to babel-node
. My confusion arises because if you write two lines of code, you can override an variable definition without an error. For example, with babel-node --presets es2015
, you can do:
> const a = 1;
undefined
> let a = 2;
undefined
Now if you write it in one line, you get an error:
> const a = 1; let a = 2;
TypeError: repl: Duplicate declaration "a"
...
It seems that in the first case, the state that a
is defined as 1
(const
variable assignment) is lost (yet not until the second assignment), while in the second case, it is maintained.
What causes the differences here? and which states are maintained?
I do not use
babel-repl
, but it must be something to do with the conversion it is doing because everything works as expected with the regular REPL:Because
const
andlet
are new syntaxes, they must be transpiled to the only binding mechanism that was available before ES6:var
. In which case,var
allows all sorts of haphazard reassignment without generating any kind of warning.So when you type an expression in
babel-node
, babel transpiles it, evaluates it, then displays the result. Babel can check for misuse of aconst
binding at transpile time, which is why you're seeing the error forconst a = 1; let a = 2
. Butconst a = 1
andlet a = 2
, when transpiled/evaluated as separate expressions, will not exhibit the error because babel is not able to detect a problem in either expression alone.A more visual demonstration of the issue: For every expression
expr
you type in thebabel-node
REPL, this is essentially what's happeningSo you won't see an error here
But you will see an error here