Why does }!{ result in False in a JavaScript console?

javascript

Solution

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.

Problem

If you put `}!{` in your JavaScript console in Chrome, as a result you will get `false`. Why don't we get an error?

Original source

Related problems