Recently, working with JavaScript in Developer Tool, I found strange feature. Chrome accepts any code between opening bracket with operator (plus, minus sign) and operator with closing brackets and executes it, like this:
I didn't find this behaviour in another browsers, just in Chrome.
Maybe it's a feature, but why and how it works, can it be problem with JavaScript engine?
Recently, working with JavaScript in Developer Tool, I found strange feature. Chrome accepts any code between opening bracket with operator (plus, minus sign) and operator with closing brackets and executes it, like this:
I didn't find this behaviour in another browsers, just in Chrome.
Maybe it's a feature, but why and how it works, can it be problem with JavaScript engine?
Share Improve this question edited Jul 11, 2015 at 16:07 Alex Saskevich asked Jul 11, 2015 at 16:00 Alex SaskevichAlex Saskevich 3511 gold badge3 silver badges13 bronze badges 2- I do like how you called it a feature and not a bug – Dan Commented Jul 11, 2015 at 16:03
- 1 I wrote a little article about it: medium./@asaskevich/… – Alex Saskevich Commented Jul 15, 2015 at 11:10
2 Answers
Reset to default 7This is the way chrome evaluates your input:
with (typeof __mandLineAPI !== 'undefined' ? __mandLineAPI : { __proto__: null }) {
// your code here...
}
So once your input is }{
it bees
with (typeof __mandLineAPI !== 'undefined' ? __mandLineAPI : { __proto__: null }) {}{} // indefined
Next input }-+{
bees
undefined -+ {} // NaN
And so on.
This happens because Chrome wraps the code you enter in the console in the following construction:
with (typeof __mandLineAPI !== 'undefined' ? __mandLineAPI : { __proto__: null }) {
// Your code
}
So, when you enter something like } 10 {
, the code evaluates to:
with (typeof __mandLineAPI !== 'undefined' ? __mandLineAPI : { __proto__: null }) {
} 10 {
}
which is empty with
block, a number, and empty structural block.
__mandLineAPI
is the internal object that contains Chrome Command Line API.