Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 27, 2020 12:51 am GMT

What is {x:1} and why does it return 1

Recently I came across what I found to be weird behavior when you type {x:1} into the respective browser developer tools (Chrome and Firefox).

It took me by surprise that Firefox returned 1. I was expecting the same response as I got in chrome namely {x:1} nicely formatted and with all the usual prototype stuff.

After some digging I think I finally figured out what's going on.

Let's go over it step by step.

First one thing to make clear {x:1} is actually not JSON as keys in JSON need to be wrapped in double quotes. (And even JSON is not considered valid javascript code)

Technically we never deal with JSON in javascript anyway except when it's in a string literal like '{ "x" : 1 }' and we feed it to JSON.parse or whatever.

Programmers mostly deal with object literals like const obj = {x:1}.

But {x:1} on its own is not an object literal. In fact it's something totally different.

If you dissect {x:1} you will see the outer part is actually a block statement.

Alt Text

A block statement (or compound statement in other languages) is used to group zero or more statements.

And within you have what's called a labeled statement

Alt Text

A labeled statement is very uncommon and not that useful. It kind of acts like a GOTO. But you can only jump to a label with a continue or break command.

loop1:for (let i = 0; i < 5; i++) {  if (i === 1) {    continue loop1;  }  str = str + i;}
Enter fullscreen mode Exit fullscreen mode

You see with the example {x:1}, x is the label name and 1 is the statement. Statements when entered into the web console are simply returned.

And that is why Firefox prints 1 when you type {x:1} into the console.

If you dig into MDN you will stumble upon this warning on object literals.

Alt Text

Okay we learned this the hard way and by detour.

But why does {x:1} do what 99.9% of developers would expect when typed into google chromes developer tools?

And the answer can be found in the source code of the actual web console.

Alt Text

souce

Chrome assumes that most developers mean to input object literals into the web console so it does a little trick.

It wraps {} with () aka the javascript grouping operator.

The grouping operator consists of a pair of parentheses around an expression or sub-expression to override the normal operator precedence so that expressions with lower precedence can be evaluated before an expression with higher priority. As it sounds, it groups what's inside of the parentheses.

So in the end what chrome executes is ({x:1}). And that for some reason still not fully clear to me (enlighten me in the comments!) turns the labeled statement within a block statement into a object literal.

And with Firefox you just have to do this manually.

Alt Text

I hope you enjoyed this investigation like I did and make sure to follow me on twitter @h43z for more goofing around.


Original Link: https://dev.to/h43z/what-is-x-1-and-why-does-it-return-1-lbj

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To