Why?} {False result in JavaScript console?

If you put }!{ In your JavaScript console in Chrome, you end up with false .

Why aren't we getting an error?

+8
javascript
source share
2 answers

The reason for this behavior is that Chrome wraps everything you enter the console with a different piece of code.

The code that it wraps (at the time of writing) is as follows:

 with ((window && window.console && window.console._commandLineAPI) || {}) { // Your code here. } 

Input }!{ Closes the bracket of the code block and creates a new (negated) object at the end.

As you can see for yourself in the console !{} Returns false .


I went through a rather lengthy investigation process to find the answer to this question, my original comments are saved below

Original answer:

Just a theory; I assume that the code entered into the console is called inside the function

function execUserCode() { code }

What are you doing creating

function execUserCode() { }!{ }

The console returns the last result, which is actually !{ } = false


Edit:

Lots of comments on how this is probably wrong. I agree. This is just a theory.

I like such puzzles, so I could break through the source of Chromium, this is a bit for me, but I will leave some pointers in case someone else gets hit.

The JS console is called the "inspector" and can be found here:

chromium/src/third_party/WebKit/Source/WebCore/inspector/

I looked at inspector/front-end/ConsoleView.js and I think I found a little where the user code is executing.

 evaluateUsingTextPrompt: function(expression, showResultOnly) { this._appendCommand(expression, this.prompt.text, false, showResultOnly); }, 

Cause!

A little brain wave. I did it 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 at chromium/src/third_party/WebKit/Source/WebCore/inspector/InjectedScriptSource.js currently around line 440.

+14
source share

Just to guess. If the script is enclosed in {} and executed using eval , this will happen.

user script: "}!{"

enclosed in {} : "{}!{}"

then eval("{}!{}") gives false

So, I think this is happening in the browser console.

+3
source share

All Articles