There are two main contexts when parsing code: the expression context and the statement context. The body of a function for example is a statement context, the right side of an assignment is an expression context. To distinguish both makes sense to forbid things like:
if( if(true) ) alert("nonsense");
Now REPL have a really difficult task: On the one hand, they have to parse functions and codeblocks that get entered, and on the other hand you sometimes just want to check how a certain object look like. Therefore this:
{ a: 1, b: 2 }
is actually a SyntaxError in JavaScript, as a {
in a statement context starts a block of code, and :
is invalid then. However the REPL is clever enough and puts that object into an expression context, and evaluates it as if it would be in parens:
({ a: 1, b: 2 })
the same happens for:
{} == {}
which is actually also a SyntaxError, however the REPL also moves it into an expression context:
({} == {})
That "moving into an expression context" is a complicated task, and it seems as if the REPL just does not see the expression here:
{} == null
and therefore parses {}
as a block. Thats the case because the REPL just naively checks if the first and the last character are {
and }
, which is the case for {} == {}
but not for {} == null
.
Relevant part of the chromium sourcecode:
if (/^\s*\{/.test(text) && /\}\s*$/.test(text))
text = '(' + text + ')';
executionContext.evaluate(text, "console", !!useCommandLineAPI, false, false, true, printResult);