<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: h43z</title>
    <description>The latest articles on DEV Community by h43z (@h43z).</description>
    <link>https://dev.to/h43z</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F90493%2F6b056cee-fc8b-47d6-b793-4dd2ef8f5bdc.jpg</url>
      <title>DEV Community: h43z</title>
      <link>https://dev.to/h43z</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/h43z"/>
    <language>en</language>
    <item>
      <title>What is {x:1} and why does it return 1 🤷‍♂️ </title>
      <dc:creator>h43z</dc:creator>
      <pubDate>Sun, 27 Dec 2020 00:51:09 +0000</pubDate>
      <link>https://dev.to/h43z/what-is-x-1-and-why-does-it-return-1-lbj</link>
      <guid>https://dev.to/h43z/what-is-x-1-and-why-does-it-return-1-lbj</guid>
      <description>&lt;p&gt;Recently I came across what I found to be &lt;em&gt;weird&lt;/em&gt; behavior when you type &lt;code&gt;{x:1}&lt;/code&gt; into the respective browser developer tools (Chrome and Firefox).&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1335570931977232385-924" src="https://platform.twitter.com/embed/Tweet.html?id=1335570931977232385"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1335570931977232385-924');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1335570931977232385&amp;amp;theme=dark"
  }



&lt;/p&gt;

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

&lt;p&gt;After some digging I think I finally figured out what's going on.&lt;/p&gt;

&lt;p&gt;Let's go over it step by step.&lt;/p&gt;

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

&lt;p&gt;Technically we never deal with JSON in javascript anyway except when it's in a &lt;strong&gt;string literal&lt;/strong&gt; like &lt;code&gt;'{ "x" : 1 }'&lt;/code&gt; and we feed it to &lt;code&gt;JSON.parse&lt;/code&gt; or whatever.&lt;/p&gt;

&lt;p&gt;Programmers mostly deal with &lt;strong&gt;object literals&lt;/strong&gt; like &lt;code&gt;const obj = {x:1}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;{x:1}&lt;/code&gt; on its &lt;em&gt;own&lt;/em&gt; is not an &lt;strong&gt;object literal&lt;/strong&gt;. In fact it's something totally different.&lt;/p&gt;

&lt;p&gt;If you dissect &lt;code&gt;{x:1}&lt;/code&gt; you will see the outer part is actually a &lt;strong&gt;block statement&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1levn8hqmshmeyte1hsl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1levn8hqmshmeyte1hsl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A block statement (or compound statement in other languages) is used to group zero or more statements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And within you have what's called a &lt;strong&gt;labeled statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2348x5l9m2dtcyuf48o1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2348x5l9m2dtcyuf48o1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;labeled statement&lt;/strong&gt; is very uncommon and not that useful. It kind of acts like a &lt;strong&gt;GOTO&lt;/strong&gt;. But you can only jump to a label with a &lt;code&gt;continue&lt;/code&gt; or &lt;code&gt;break&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;loop1:
for (let i = 0; i &amp;lt; 5; i++) {
  if (i === 1) {
    continue loop1;
  }
  str = str + i;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You see with the example &lt;code&gt;{x:1}&lt;/code&gt;, &lt;code&gt;x&lt;/code&gt; is the label name and &lt;code&gt;1&lt;/code&gt; is the statement. Statements when entered into the web console are simply returned.&lt;/p&gt;

&lt;p&gt;And that is why Firefox prints &lt;code&gt;1&lt;/code&gt; when you type &lt;code&gt;{x:1}&lt;/code&gt; into the console.&lt;/p&gt;

&lt;p&gt;If you dig into MDN you will stumble upon this warning on &lt;strong&gt;object literals&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgszm8jxlyke99j514hdr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgszm8jxlyke99j514hdr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay we learned this the hard way and by detour.&lt;/p&gt;

&lt;p&gt;But why does &lt;code&gt;{x:1}&lt;/code&gt; do what 99.9% of developers would expect when typed into google chromes developer tools?&lt;/p&gt;

&lt;p&gt;And the answer can be found in the source code of the actual web console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr6v0k5o0m0mk42ar3f4l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr6v0k5o0m0mk42ar3f4l.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://chromium.googlesource.com/chromium/src.git/+/4fd348fdb9c0b3842829acdfb2b82c86dacd8e0a%5E!/#F2" rel="noopener noreferrer"&gt;souce&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Chrome assumes that most developers mean to input &lt;strong&gt;object literals&lt;/strong&gt; into the web console so it does a little trick.&lt;/p&gt;

&lt;p&gt;It wraps &lt;code&gt;{}&lt;/code&gt;  with &lt;code&gt;()&lt;/code&gt; aka the javascript &lt;strong&gt;grouping operator&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;And with Firefox you just have to do this manually.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flne1wud5s7np2asfrkdk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flne1wud5s7np2asfrkdk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this investigation like I did and make sure to follow me on twitter &lt;a href="https://twitter.com/h43z" rel="noopener noreferrer"&gt;@h43z&lt;/a&gt; for more goofing around. &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
