The reason you see such problems is because the console itself is trying to emulate the global scope of the context that you are currently targeting. It also tries to capture return values from statements and expressions that you write on the console so that they appear as results. Take for example:
> 3 + 2 < 5
Here it is executed as if it were an expression, but you wrote it as if it were a statement. In normal scenarios, the value will be discarded, but here the code must be internally distorted (for example, wrapping the entire operator with the function context and return ), which causes all kinds of strange effects, including problems, re experience.
This is also one of the reasons why some open source ES6 scripts work fine, but not in the Chrome Dev Tools console.
Try this in Node and the Chrome console:
{ let a = 3 }
In the Node or <script> it works fine, but in the console it gives Uncaught SyntaxError: Unexpected identifier . It also gives you a source link in the form of VMxxx:1 , which you can click to check the estimated source, which displays as:
({ let a = 3 })
So why did this?
The answer is that it must convert your code to an expression so that the result can be returned to the caller and displayed in the console. You can do this by wrapping the operator in parentheses, which makes it an expression, but also makes the block above syntactically incorrect (the expression cannot have a block declaration).
The console is trying to fix these edge cases by being smart about the code, but this is beyond the scope of this answer, I think. You can specify an error to make sure that it will be fixed.
Here is a good example of something very similar:
stack overflow
The safest way to get your code to work is to make sure that it can be run as an expression and check the SyntaxError source link to find out what the actual execution code is, and reconstruct the solution from this. This usually means a pair of strategically placed parentheses.
In short: the console tries to emulate the global execution context as accurately as possible, but due to limitations of interaction with the v8 engine and JavaScript semantics, this is sometimes difficult or impossible to solve.