The reason for this behaviour is because Chrome wraps anything you enter into the console with another piece of code.
The code it wraps with (at the time of writing) is as follows:
with ((window && window.console && window.console._commandLineAPI) || {}) {
// Your code here.
}
Entering }!{
closes the code block brace, and makes a new (negated) object at the end.
As you can see for youself in the console !{}
returns false
.
I went through quite a lengthy investigative process to find the answer to this, my original comments are preserved below
Original Answer:
Just a theory; I imagine the code entered in the console being called inside a function
function execUserCode() { code }
What you're doing is creating
function execUserCode() { }!{ }
The console is returning the last result, which is actually !{ }
= false
Edit:
Lots of comments about how this is probably wrong. I agree. It's just a theory.
I like these kind of puzzles so I had a dig through the Chromium source, it's a bit much for me but I'll leave some pointers in case anyone else has a stab.
The JS console is called the "inspector" and can be found here:
chromium/src/third_party/WebKit/Source/WebCore/inspector/
I've been looking at inspector/front-end/ConsoleView.js
and I think I found a bit of where the user code is executed.
evaluateUsingTextPrompt: function(expression, showResultOnly)
{
this._appendCommand(expression, this.prompt.text, false, showResultOnly);
},
The reason!
Little brainwave. I did this in the console
> myEval = eval
> eval = function(str) { console.log(str); myEval(str) }
> }!{
Result:
with ((window && window.console && window.console._commandLineAPI) || {}) {
}!{
}
I was close, but now we have the answer :)
The code is generated in chromium/src/third_party/WebKit/Source/WebCore/inspector/InjectedScriptSource.js
currently around line 440.
{} != ({})
This is a syntax error.
SyntaxError: Unexpected token !=
{}
is ambigious like that. Is it an empty block, or an empty object literal? It's failing because a comparison operator can not follow a block.
Wrapping it with parenthesis makes the parser treat it as an expression. An expression can't contain a block, so it knows it's an object.
However, if you wrap that comparison in an expression...
({} != ({}))
...it's still true because variables which have an object assigned to them's values are a pointer to them and as a consequence, they are never copied around by their contents (though this is irrelevant to your example). Because of this, their pointer is always different and the comparison fails.
This also implies that comparing two variables which point to the same object will work as expected, as their pointers will be the same.
Best Answer
I believe that's because plain
{}[true]
is parsed as an empty statement block (not an object literal) followed by an array containingtrue
, which istrue
.On the other hand, applying the
!
operator makes the parser interpret{}
as an object literal, so the following{}[true]
becomes a member access that returnsundefined
, and!{}[true]
is indeedtrue
(as!undefined
istrue
).