What is {x:1} and why does it return 1 🤷‍♂️ 
Tags: javascript
Published: 2020-12-27 00:51:09

---
title: What is {x:1} and why does it return 1 🤷‍♂️ 
published: true
description: 
tags: javascript
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/31nndzm2q048i1vapmmr.png
---

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).

{% twitter 1335570931977232385 %}

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](https://dev-to-uploads.s3.amazonaws.com/i/1levn8hqmshmeyte1hsl.png)

> 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](https://dev-to-uploads.s3.amazonaws.com/i/2348x5l9m2dtcyuf48o1.png)

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;
}
```

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](https://dev-to-uploads.s3.amazonaws.com/i/gszm8jxlyke99j514hdr.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/r6v0k5o0m0mk42ar3f4l.png)

[souce](https://chromium.googlesource.com/chromium/src.git/+/4fd348fdb9c0b3842829acdfb2b82c86dacd8e0a%5E!/#F2)

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](https://dev-to-uploads.s3.amazonaws.com/i/lne1wud5s7np2asfrkdk.png)

I hope you enjoyed this investigation like I did and make sure to follow me on twitter [@h43z](https://twitter.com/h43z) for more goofing around.