Tuesday 30 May 2017

What state is kept between JavaScript lines?

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?



via tinlyx

No comments:

Post a Comment