<?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: Ahmet Kapusuz</title>
    <description>The latest articles on DEV Community by Ahmet Kapusuz (@ahmetkapusuz).</description>
    <link>https://dev.to/ahmetkapusuz</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%2F223594%2Fc05c6f9d-d486-409d-b09f-b95a77aeffd9.jpeg</url>
      <title>DEV Community: Ahmet Kapusuz</title>
      <link>https://dev.to/ahmetkapusuz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmetkapusuz"/>
    <language>en</language>
    <item>
      <title>What is Optional Chaining in JavaScript?</title>
      <dc:creator>Ahmet Kapusuz</dc:creator>
      <pubDate>Fri, 13 Dec 2019 21:44:00 +0000</pubDate>
      <link>https://dev.to/ahmetkapusuz/what-is-optional-chaining-in-javascript-52ik</link>
      <guid>https://dev.to/ahmetkapusuz/what-is-optional-chaining-in-javascript-52ik</guid>
      <description>&lt;p&gt;At the time of writing this blog post, optional chaining is reached stage4 in TC39 proposals and probably will be included in ES2020. Optional chaining is a new feature that can make your JavaScript code look more clear.&lt;/p&gt;

&lt;p&gt;When you want to reach a property of an object, usually you can use &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator to avoid getting errors when the object is null or undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this new JavaScript feature this syntax become better and more clear than the one above.&lt;br&gt;
You can just use &lt;code&gt;?.&lt;/code&gt; instead of adding &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator for each level of the tree.&lt;/p&gt;

&lt;p&gt;Code above can be written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If user or address is undefined or null, then the value of city become undefined.&lt;br&gt;
If you want to experiment this feature, you can use &lt;a href="https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining"&gt;this Babel plugin&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another new feature I liked is Nullish Coalescing feature. It is kind of a complementary feature for optional chaining and also planned to be released in ES2020.&lt;/p&gt;




&lt;p&gt;You can also read this post in &lt;a href="https://blog.ahmetkapusuz.com/what-is-optional-chaining-in-javascript/"&gt;my blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es2020</category>
      <category>esnext</category>
    </item>
    <item>
      <title>What is the 'Temporal Dead Zone' in JavaScript?</title>
      <dc:creator>Ahmet Kapusuz</dc:creator>
      <pubDate>Fri, 29 Nov 2019 19:24:28 +0000</pubDate>
      <link>https://dev.to/ahmetkapusuz/what-is-the-temporal-dead-zone-in-javascript-3hlc</link>
      <guid>https://dev.to/ahmetkapusuz/what-is-the-temporal-dead-zone-in-javascript-3hlc</guid>
      <description>&lt;p&gt;This concept is probably not something that you come across too often and sounds a little bit weird. But it can be useful to know some details to avoid possible bugs or problems in your code.&lt;/p&gt;

&lt;p&gt;So, let's look at the example below. It's easy to see that it will print &lt;code&gt;foo&lt;/code&gt; to console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// foo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
What if we change the order of the lines as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you will see that it prints &lt;code&gt;undefined&lt;/code&gt;. This is because the &lt;code&gt;var&lt;/code&gt; declaration is hoisted but the value of variable is undefined when console.log line is executed.&lt;/p&gt;

&lt;p&gt;Now let's add a bit of ES6 to our simple example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run this code on your console, you will get a &lt;code&gt;ReferenceError&lt;/code&gt;. This is one of the main differences between &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;/&lt;code&gt;const&lt;/code&gt;. When &lt;code&gt;let&lt;/code&gt;/&lt;code&gt;const&lt;/code&gt; variables are accessed before their declaration, they throw a ReferenceError instead of having undefined value.&lt;/p&gt;

&lt;p&gt;You may think that &lt;code&gt;let&lt;/code&gt;/&lt;code&gt;const&lt;/code&gt; variables are not hoisted and we're getting ReferenceError because of this, but this is not correct.&lt;/p&gt;

&lt;p&gt;Let's explain that in a little bit more complex example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you think the code above will print to console?&lt;/p&gt;

&lt;p&gt;The answer is &lt;code&gt;undefined&lt;/code&gt;. This is a simple example that explains how hoisting works in JavaScript. &lt;code&gt;foo&lt;/code&gt; variable is hoisted inside of the function scope but it's not initialized where the console.log line is executed, so it prints &lt;code&gt;undefined&lt;/code&gt;. This is the expected result.&lt;/p&gt;

&lt;p&gt;So what if we change it as below?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code above will throw a &lt;code&gt;ReferenceError&lt;/code&gt; similar to the &lt;code&gt;let&lt;/code&gt; example before. This is because &lt;code&gt;let&lt;/code&gt;/&lt;code&gt;const&lt;/code&gt; variables are actually hoisted, but they throw &lt;code&gt;ReferenceError&lt;/code&gt; if they're accessed before being initialized. The variable is in a "temporal dead zone" from the start of the block until the initialization is processed.&lt;/p&gt;

&lt;p&gt;If you want to dig deeper you can see MDN documentation of &lt;code&gt;let&lt;/code&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone"&gt;here&lt;/a&gt;, and ES specification &lt;a href="https://www.ecma-international.org/ecma-262/6.0/#sec-let-and-const-declarations"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There is a misconception that says let/const are not hoisted in JavaScript, we cannot say that is correct. According to &lt;a href="https://www.ecma-international.org/ecma-262/9.0/index.html#sec-let-and-const-declarations"&gt;ES6 specification&lt;/a&gt;: &lt;em&gt;The variables are created when their containing Lexical Environment is instantiated but may not be accessed in any way until the variable’s LexicalBinding is evaluated.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Note: You can see another great explanation of hoisting of let/const &lt;a href="https://stackoverflow.com/questions/31219420/are-variables-declared-with-let-or-const-not-hoisted-in-es6/31222689#31222689"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;You can also read this post in &lt;a href="https://blog.ahmetkapusuz.com/what-is-the-temporal-dead-zone-in-javascript/"&gt;my blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>webdev</category>
    </item>
    <item>
      <title>My first Svelte app</title>
      <dc:creator>Ahmet Kapusuz</dc:creator>
      <pubDate>Mon, 25 Nov 2019 22:27:50 +0000</pubDate>
      <link>https://dev.to/ahmetkapusuz/my-first-svelte-app-3dbl</link>
      <guid>https://dev.to/ahmetkapusuz/my-first-svelte-app-3dbl</guid>
      <description>&lt;p&gt;I've created my first Svelte application. My first impression is that it's really easy to get started if you have experience on other frontend frameworks.&lt;br&gt;
I've spent a few hours to figure things out and successfully create a working app. &lt;/p&gt;

&lt;p&gt;I've used dev.to api to fetch articles, you can search by any tag and results will be displayed. You can see it here: &lt;a href="https://codesandbox.io/s/sveltedevtoapp-yrg6r"&gt;https://codesandbox.io/s/sveltedevtoapp-yrg6r&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know that some parts of the code is not ideal and probably I'm doing many things wrong. Let me know if you see something really weird. &lt;/p&gt;

&lt;p&gt;My overall experience coding a Svelte app was really great until now. &lt;br&gt;
I will definitely keep learning about it and create a bigger application in the future. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>svelte</category>
      <category>beginners</category>
      <category>devtoapi</category>
    </item>
  </channel>
</rss>
