<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ES6+ Archives - I Learn Javascript</title>
	<atom:link href="https://www.ilearnjavascript.com/category/javascript/es6/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.ilearnjavascript.com/category/javascript/es6/</link>
	<description>Learn Javascript for free</description>
	<lastBuildDate>Wed, 14 Aug 2019 16:39:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>ES8: async await</title>
		<link>https://www.ilearnjavascript.com/es8-async-await/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Mon, 11 Mar 2019 22:41:00 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=1024</guid>

					<description><![CDATA[<p>Async await improves how we handle asynchronous code. It is built on top of ES6 Promises and aims to give a cleaner and more concise syntax.</p>
<p>The post <a href="https://www.ilearnjavascript.com/es8-async-await/">ES8: async await</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975">
.flex_column.av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975 av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-eea419eebf352b6340575a7a24eb24a1">
#top .av-special-heading.av-av_heading-eea419eebf352b6340575a7a24eb24a1{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-eea419eebf352b6340575a7a24eb24a1 .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-eea419eebf352b6340575a7a24eb24a1 .av-subheading{
font-size:14px;
}
</style>
<div  class='av-special-heading av-av_heading-eea419eebf352b6340575a7a24eb24a1 av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES8: async await</h1><div class='av-subheading av-subheading_below'><p>7min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-3rzn2o-833a4470f9295a6b247b6e1e43c06906 '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>Async await improves how we handle asynchronous code. It is built on top of <a href="https://www.ilearnjavascript.com/es6-promises/" target="_blank" rel="noopener noreferrer">ES6 Promises</a> (our rescue from <a href="/glossary/#toggle-id-4" target="_blank" rel="noopener noreferrer">callback hell</a>) and aims to give a cleaner and more concise syntax. Similar to <a href="https://www.ilearnjavascript.com/es6-classes/" target="_blank" rel="noopener noreferrer">ES6 Classes</a> and prototypal inheritance the async await syntax is basically syntactic sugar for Promises.</p>
<h2>How it works</h2>
<p>The syntax for async await is actually quite intuitive. We use the keyword <em>async</em> to define an asynchronous function. Within the async function we can use the keyword <em>await</em> to literally wait until a Promise is resolved/ rejected (note: fetch also returns a Promise). Another benefit of async await is that we can use try/ catch statements as in synchronous code to do our error handling.</p>
<p>In the following example we will create an async function, fetch the random joke API and await the returned Promise from the API call to be resolved and parsed to json. Then we call the async function getJoke and log the data we receive as soon as getJoke is processed and has a value returned to us. We execute this in a try/ catch statement to do the error handling in case the API call is rejected.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/13bc1c87fa0cee1f195a042c997c37c5">Gist</a>.</p>
<h2>Async await is non blocking</h2>
<p>Async await is non blocking code. This is especially important as the Browser has only one single thread for all the tasks (parse HTML, apply CSS, render layout, execute Javascript etc.) and Javascript code is &#8220;usually&#8221; blocking. Using async code can help to improve the performance and user experience of an app/ website.</p>
<p>In the following example we will create a Promise which will resolve after 2 seconds, clearing our interval &#8220;tick&#8221;. Then we create an async function asyncCall which calls our Promise resolveAfter2Seconds and awaits its result &#8220;resolved&#8221; and logs it. The interval tick runs every 100ms and logs &#8220;tick&#8221; to the console. Immediately after the tick interval is set up, asyncCall is executed. Since setInterval is by default also asynchronous and the way the Javascript event loop works, &#8220;calling&#8221; from within the asyncCall function gets logged before the first &#8220;tick&#8221; is logged.</p>
<p>Tick will log exactly 20 times (2000ms) until the Promise is resolved and the interval is cleared. This example shows that async await handles the Promise in the background until it is resolved/ rejected.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/28afd4e5345e983f0031112d05c670e4">Gist</a>.</p>
<h2>Callbacks vs Promises vs async await</h2>
<p>Async await gives us arguably a nicer and more concise syntax over Promises and unquestionable over multiple nested callbacks. But judge for yourself comparing callbacks, Promises and async await:</p>
<h3>Callback(hell)</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/6967a814aa877a42f2cb3478a1288be8">Gist</a>.</p>
<h3>ES6 Promises</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/80d82902b8a0a51927330e46d46baacc">Gist</a>.</p>
<h3>Async await</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/a0af3c4d06f201207d8c7eb61b81e7e7">Gist</a>.</p>
<p>I am personally a big fan of async await and thanks to Babel it is easy integratable into our code. For everyone who doesn&#8217;t transpile their code: async await is not fully supported yet (and won&#8217;t be as long as we need to support IE11. See: <a href="https://caniuse.com/#search=async">https://caniuse.com/#search=async</a>)</p>
<p>Let me know what you think in the comments.</p>
</div></section><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756">
#top .hr.hr-invisible.av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756{
height:50px;
}
</style>
<div  class='hr av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756 hr-invisible  avia-builder-el-3  el_after_av_textblock  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-2j8p8w-410812da681fe4d135e1c91fb9db8328'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es8-async-await/">ES8: async await</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ES6: Modules</title>
		<link>https://www.ilearnjavascript.com/es6-modules/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Sun, 10 Mar 2019 09:27:11 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=796</guid>

					<description><![CDATA[<p>With ECMAScript 2015 (ES6) Modules become finally native to modern Browsers. In this article I will explain how you can use them in your projects.</p>
<p>The post <a href="https://www.ilearnjavascript.com/es6-modules/">ES6: Modules</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975">
.flex_column.av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975 av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-adc9f3652e958ac1a6f75e8f4e9a0396">
#top .av-special-heading.av-av_heading-adc9f3652e958ac1a6f75e8f4e9a0396{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-adc9f3652e958ac1a6f75e8f4e9a0396 .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-adc9f3652e958ac1a6f75e8f4e9a0396 .av-subheading{
font-size:14px;
}
</style>
<div  class='av-special-heading av-av_heading-adc9f3652e958ac1a6f75e8f4e9a0396 av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES6: Modules</h1><div class='av-subheading av-subheading_below'><p>8min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-gs2r8-904cacc4c99d2dbf28bd17b8c81e275c '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>With ECMAScript 2015 (ES6) Modules become finally native to modern Browsers. In JavaScript Modules represent small units of independent, reusable code. They essentially make our code more maintainable and flexible. One file could contain one or multiple Modules. We can import and export them as per need. Therefore multiple Modules combined could also make up a larger Module. They are the foundation of many JavaScript design patterns and are critically necessary when building any non-trivial JavaScript-based application.</p>
<p>This being said, the concept of Modules itself is not new to the Javascript community. Developers so far usually fall back to one of the following options</p>
<ul>
<li>load multiple Javascript files into the Document (very bad for performance because each file needs to be requested and served individually)</li>
<li>use separate Javascript files for Development and concatenate them into one file for Production (better performance-wise but fairly hard to maintain)</li>
</ul>
<p>or the modern approach</p>
<ul>
<li>use a Library such as CommonJS or AMD (requireJS) to do the job (defacto standard which tools like Webpack make use of)</li>
</ul>
<h2>How it works</h2>
<p>We declare our script tag with type &#8220;module&#8221; and let the Browser this way know that it is an ES6 Module.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/c81dbe94979f3db33ddced7719a1af90">Gist</a>.</p>
<p>We differentiate between named exports and default exports. We can have multiple named exports per file and one default export per file. With the keyword <em>export</em> we let the Javascript engine know that we want to export this Variable, Object, Array, Function or Class (technically also a Function). ES6 Modules are automatically strict-mode code, even if we don’t declare them with &#8220;use strict&#8221;. Also, everything declared within a Module is scoped to this Module and not automatically Global. The Scope in Javascript from top to bottom is:</p>
<ul>
<li>Global scope</li>
<li>Module scope</li>
<li>Function scope</li>
<li>Block scope</li>
</ul>
<h3>Named Export</h3>
<p>In the following example we will export multiple named variables, each representing a different Module. For simplicity we will store them all in one file but each of them could generally also be in it&#8217;s own file.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/5b87bd7c80bbdd1ac20cc4bc77695ce0">Gist</a>.</p>
<h3>Import named export</h3>
<p>In our index.js file we <em>import</em> module1, module2 and module3 from modules.js. When we <em>import</em> them as named exports we need to enclose them in brackets and use the correct name that was specified in the export. Then we will insert them into our index.html Document.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/70dc1a9c02d0a3b34f096b16d44afb09">Gist</a>.</p>
<h3>Default Exports</h3>
<p>When we make a default export we don&#8217;t specify a name. We just write</p>
<ul>
<li><em>export default function foo()</em></li>
<li><em>export default { foo: &#8216;bar&#8217; }</em></li>
<li><em>export default [&#8216;arr1‘]</em></li>
<li><em>export default &#8216;string&#8217;</em></li>
</ul>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/87bae57783d94041d3525816f3bbd72c">Gist</a>.</p>
<h3>Import default export</h3>
<p>When we import a default export we don&#8217;t enclose it in brackets and can choose any name for it. In our example we will call it defaultModule for clarity.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/9f3b7b1aa6e4badcc5694477be939584">Gist</a>.</p>
<p>ES6 Modules, allthough becoming now native to modern Browsers, will at the current stage most probably not change how code is organized in most existing projects. There is no real benefit of replacing native ES6 Modules with whatever Library is currently used. Especially since Browsers such as IE11 and older Browserversions of modern Browsers don&#8217;t support ES6 Modules at this time fully and therefore require some kind of fallback: <a href="https://caniuse.com/#search=modules">https://caniuse.com/#search=modules</a></p>
<p>But it is only a matter of time until Modules are going to be the new standard and especially for smaller projects that don&#8217;t have an elaborate build process it could be a real benefit to use the native implementation.</p>
</div></section><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756">
#top .hr.hr-invisible.av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756{
height:50px;
}
</style>
<div  class='hr av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756 hr-invisible  avia-builder-el-3  el_after_av_textblock  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-9vxvw-e2fcc71aac98f2d4e886686b1f563976'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es6-modules/">ES6: Modules</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ES6: Rest Parameter</title>
		<link>https://www.ilearnjavascript.com/es6-rest-parameter/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Sun, 03 Mar 2019 11:52:17 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=718</guid>

					<description><![CDATA[<p>The Rest Paramameter collects all the remaining elements into an array. It is a sibling of the Spread Operator and uses the same syntax of three dots "...".</p>
<p>The post <a href="https://www.ilearnjavascript.com/es6-rest-parameter/">ES6: Rest Parameter</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975">
.flex_column.av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975 av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-2bf2dd9cc7a03e10963953850e4ba35b">
#top .av-special-heading.av-av_heading-2bf2dd9cc7a03e10963953850e4ba35b{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-2bf2dd9cc7a03e10963953850e4ba35b .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-2bf2dd9cc7a03e10963953850e4ba35b .av-subheading{
font-size:14px;
}
</style>
<div  class='av-special-heading av-av_heading-2bf2dd9cc7a03e10963953850e4ba35b av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES6: Rest Parameter</h1><div class='av-subheading av-subheading_below'><p>5min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-jssv0bgo-2bba0c68031560dc18f0dbe9654fc1d9 '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>The Rest Paramameter collects all the remaining elements into an array. It is the sibling of the <a href="https://www.ilearnjavascript.com/es6-spread-operator/">Spread Operator</a> and uses the same syntax of three dots &#8220;&#8230;&#8221; preceeding it.</p>
<h2>Sum up function parameters</h2>
<p>The typical usecase for the Rest Paramter is to sum up all remaining parameters of a function which we don&#8217;t want to specify. In this case we sum up all the adress data into one array.</p>
<p><strong>Note</strong>: The Rest Parameter can only be used at the end after all other parameters have been specified.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/2e0b6d9c21ba434849a40644f2cb5c95">Gist</a>.</p>
<p>If we want to output the adress as a String instead of an array we can use the <a href="https://www.ilearnjavascript.com/es6-spread-operator/">Spread Operator</a>to do it.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/8c70d3a3e366a22ed837a934366c8354">Gist</a>.</p>
<h2>Rest Paramter vs Arguments Object</h2>
<p>Dont cofuse the Rest Paramter with the Arguments Object in Javascript. A short recap: The Arguments object allows us to access all arguments that have been passed into the function in form of an array-like object.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/e6b3324b9c7a293e76e7a844594869e9">Gist</a>.</p>
<p>There are three main differences:</p>
<ul>
<li>The arguments object contains all arguments passed to the function while the Rest Parameter only contains the one&#8217;s that haven&#8217;t been specified at the end of the parameters list.</li>
<li>The arguments object is in contrary to the Rest Parameter only an array-like object which means it inherits not the same methods (e.g. sort, map, filter) as a real array. The Rest Paramter is an instance of the Array Object and therefor has full access to all its methods.</li>
<li>the arguments object has additional functionality specific to itself (like the calleeproperty).</li>
</ul>
<h2>Rest Parameter in Destructuring</h2>
<p>The Rest Paramater comes in really handy when used with <a href="https://www.ilearnjavascript.com/es6-descructuring/">Destructuring</a> to collect all non-specified values in an array.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/732b7a454734ea42ce7fc13ba327ce1f">Gist</a>.</p>
</div></section><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756">
#top .hr.hr-invisible.av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756{
height:50px;
}
</style>
<div  class='hr av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756 hr-invisible  avia-builder-el-3  el_after_av_textblock  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-15jxa-39b448e935f3a8616855caac3db425ae'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es6-rest-parameter/">ES6: Rest Parameter</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ES6: Classes</title>
		<link>https://www.ilearnjavascript.com/es6-classes/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Sun, 03 Mar 2019 10:54:55 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=700</guid>

					<description><![CDATA[<p>Classes in Javascript - Sugarcoat for prototypal inheritance.</p>
<p>The post <a href="https://www.ilearnjavascript.com/es6-classes/">ES6: Classes</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975">
.flex_column.av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975 av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-df9d9d1fae884479b87de8f00f1a544a">
#top .av-special-heading.av-av_heading-df9d9d1fae884479b87de8f00f1a544a{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-df9d9d1fae884479b87de8f00f1a544a .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-df9d9d1fae884479b87de8f00f1a544a .av-subheading{
font-size:14px;
}
</style>
<div  class='av-special-heading av-av_heading-df9d9d1fae884479b87de8f00f1a544a av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES6: Classes</h1><div class='av-subheading av-subheading_below'><p>20min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-jssstan3-903ca3be2179c219893a588554cc5b06 '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>Object-oriented programming languages like PHP, Java or C++ have natively classes built-in to their language. Don&#8217;t get fooled by the new ES6 class syntax. Javascript doesn&#8217;t have this feature but rather makes use of its powerful prototypal inheritance concept to &#8220;simulate&#8221; them or in nicer words &#8220;sugarcoat&#8221; it. In Javascript you can program both object-oriented and functional because prototyal inheritance is extremly powerful.</p>
<p>This article will start with a quick recap on prototypal inheritance in Javascript, feel free to jump directly to the <a href="#class-anchor">new class concept</a>.</p>
<h2>Prototypal inheritance</h2>
<p>Prototypal inheritance and object orientation are complex topics and could easily each on their own fill multiple long articles. This is just a quick recap of the concepts behind.</p>
<p>Lets start with a person object john that has a greeting method that ouputs its name. Remember: if we have a method on an object the <em>this</em> keyword will always point to the object itself.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/aa2d0cefe10690d9654ea18843205ded">Gist</a>.</p>
<p>Lets say now we need to make another person object Jane. How shall we do it? Just copy the previous one and give it different values? What if we need 10 or 100 different person objects? Copy and paste them all with different values?</p>
<p>Allthough it would technically work it really shouldn&#8217;t be how a developer approach this task. There is a fundamental concept that is universal to programming which is called <strong>Dont Repeat Yourself (DRY)</strong>. So lets stick to it and make our code dynamic by creating a function that serves us as a blue-print for unlimited person object instances.</p>
<h3>Object Constructor Function</h3>
<p>First we need to create an object constructor function with the properties we want our person objects to have. Object constructor functions are normal functions which are invoked with the <em>new</em> keyword preceeding it. We define its properties with <em>this.propertyname = propertyname</em>.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/9d5f13b976470150cd2034a86c173baf">Gist</a>.</p>
<p>In Javascript every function is an object but not every object is a function. Higher-order functions are functions that accept other functions as arguments which is only possible if functions are treated like objects. In order to make an instance of an object we call the object constructor function we want to inherit from with the <em>new</em> keyword beforehand. This way the Javascript engine knows that we intend to create a new instance of that object.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/7636128cf99dd4c20bf87d7353dc3e28">Gist</a>.</p>
<h3>Adding methods to all instances</h3>
<p>Now its time to add our greet method to the object constructor function. We want to add it to the object constructor function (our blue-print object) that all instances have access to. We can do this in two ways:</p>
<p>Adding the method from the very beginning directly to our object constructor function (same as we add properties)&#8230;</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/d139b9871b392a8b32f59690c8c38bcb">Gist</a>.</p>
<p>&#8230; or add it to the prototype of the object constructor function. Each function has a prototype property object with a constructor method that points to the function itself. Whenever we create a new instance (keyword new) we automatically invoke this constructor method which lives on the prototype of the object constructor function.</p>
<p>Every plain object has a __proto__ object and the __proto__ object of objects created as instances of another object (like in our example) point to the prototype of the object it is created from (remember: All functions are objects but not all objects are functions).</p>
<p>What is important here: Instances of objects (like john) have a reference to the objects they inherit from (like es5_Person). John&#8217;s __proto__ points to the prototype of es5_Person. One could also say that john&#8217;s __proto__ gets replaced with the prototype of its parent and therefor inherits all its methods. Both objects point to the same spot in memory. Their destinies are linked from now on. If you change the parents prototype you will also change the __proto__ of all their instances.</p>
<p>This also means if we add a method to the prototype of the object constructor function (es5_person) all their instances (john) will have access to it. Even if they are already created.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/08b59458eeed32bc9a8391eb71d4cbde">Gist</a>.</p>
<p>This works because when we call the method greet on john the Javascript engine looks if it can finds it on the john object first. If it is not present there (like in our example) it goes down the &#8220;prototype chain&#8221;. Since johns __proto__ points to the es5_Person prototype the Javascript Engine will find the method there and can run it.</p>
<h2 id="class-anchor">Classes</h2>
<p>Now that we understood how prototypal inheritance work lets look at ES6 classes but remember: under the hood it is still prototypal inheritance. It starts with the <em>class</em> keyword and the name of our class. It is written without parenthesis, has a constructor function and any method will be written just with its methodname, followed by parenthesis without comma seperation inbetween them.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/7a95fc91a76971af13d586a5ec4b1670">Gist</a>.</p>
<h2>Subclasses</h2>
<p>A subclass extends the functionality of its parent class. All instances of a subclass own the full functionality of the base class but additionally have methods which only instances of this subclass have access to. Let&#8217;s define a soldier subclass. Every soldier is a also a person but not every person is a soldier. Let&#8217;s look at it first how it was done in ES5 and how it is done now in ES6. Jump directly to <a href="#anchor-subclasses">ES6 Subclasses</a> if you don&#8217;t need this recap.</p>
<h3>ES5</h3>
<p>In ES5 we would create a new object constructor function with all the properties we want to inherit from the base class (firstname, lastname) and additonally all the properties that only exist in the subclass (weapon). Then we would call the base class within the context of the soldier subclass via <em>call</em> or <em>apply </em>and pass the current this context as well as all the properties of the base class (firstname, lastname). Afterwards we set the property only relevant for the subclass (weapon) normally how we are used to it in the object constructor function.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/7ee283b8b74f8b435494d73077426c81">Gist</a>.</p>
<p>Now we have a subclass who&#8217;s instances have the properties firstname, lastname and weapon but we dont inherit the methods from es5_Person. We don&#8217;t have access to its prototype. One solution is to use Object.create() to create a new object on the es5_Soldier.prototype and pass it the es5_Person.prototype. The Object.create() method creates a new object, using an existing object as the prototype of the newly created object. This means on the es5_Soldier.prototype a new object is created which has the es5_person.prototype as its __proto__. This way all instances of es5_Soldier are able to access the methods of es5_Person through the prototype chain.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/6c16bb7a1186882bfb0e2a67e9c96049">Gist</a>.</p>
<p>Any method which we add now to the prototype of our subclass will live there while the methods of its base class can be accessed through the __proto__ of the prototype of the subclass. Since the Javascript Engine goes down the prototype-chain automatically when it doesn&#8217;t find it we can call our methods as we are used to on the object. Let&#8217;s add a method for all our soldier instances.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/b3048557464e31fcabbd32908af6e4a4">Gist</a>.</p>
<p>Lets now create a new es5_Soldier instance &#8220;jackTheSoldier&#8221; and try if we can access both the shoot() method of its parent class and the greet() method of the base class es5_Person.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/ed25ff623e3d4c7bf0e31805812ffd6e">Gist</a>.</p>
<p>There is also another way to access the greet method from es5_Person but with Object.create(). We can define <em>greet()</em> as a new method on the es5_Soldier.prototype and call from within the function the es5_Person.prototype.greet() method and apply the current <em>this</em> context.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/02017e7e0bc15abe4d07cada1b67a626">Gist</a>.</p>
<p>Both is possible but I personally prefer the Object.create() method as it feels more straightforward to me.</p>
<p>Last but not least we can also prove that john can <em>greet()</em> but not <em>shoot()</em> because it has as an instance of es5_Person only access to <em>greet()</em>. Only instances of es5_Soldier can both <em>greet()</em> and <em>shoot()</em>.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/f351a9491d375a16b62501a37d2096f1">Gist</a>.</p>
<h3 id="anchor-subclasses">ES6</h3>
<p>In the ES6 class syntax the subclass concept is hugely simplified which is why a lot of developer like it. But remember it is all still prototypal inheritance under the hood. If we want to make es6_Soldier a subclass of es6_person we just use the keyword <em>extends</em>.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/4c40959317c49f5681ebebb87b825546">Gist</a>.</p>
<p>Since our es6_Soldier subclass adds a new property (weapon) we have to manually call the constructor function, pass it the base class properties firstname + lastname and then add weapon as a new parameter. The <em>super()</em> method is built-in and is used to call the parents constructor in our current context. If we wouldn&#8217;t add any new parameters we could leave it out because&#8230;</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/11fadf62f9dd0767aa22f0b16c439d53">Gist</a>.</p>
<p>&#8230;would be called otherwise automatically in the class.</p>
<p>If we want to override a parent method we can simply define it with the same name in our subclass and because within the prototype chain it will be before the parent method it will get run. If we however want to build on top of it, we also define it first in our subclass but also call the parent&#8217;s method from within via super.methodName(). Overwriting the greet() method from the es6_Person class within the es6_Soldier subclass would look like this:</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/93fb82685126f095c35baf624aa51cb6">Gist</a>.</p>
<p>This is it about classes. If you have any feedback or questions please let me know in the comments.</p>
</div></section><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756">
#top .hr.hr-invisible.av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756{
height:50px;
}
</style>
<div  class='hr av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756 hr-invisible  avia-builder-el-3  el_after_av_textblock  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-72pis-30bb8351bf6d90489b0835edcf022e4d'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es6-classes/">ES6: Classes</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ES6: Enhanced Object Literals</title>
		<link>https://www.ilearnjavascript.com/es6-enhanced-object-literals/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Sun, 03 Mar 2019 00:49:05 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=690</guid>

					<description><![CDATA[<p>ES6 brings us a better version of object literals. We will write less code to reach the same result.</p>
<p>The post <a href="https://www.ilearnjavascript.com/es6-enhanced-object-literals/">ES6: Enhanced Object Literals</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975">
.flex_column.av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975 av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-5161523c1b8041f37473c6bfd0a68dbd">
#top .av-special-heading.av-av_heading-5161523c1b8041f37473c6bfd0a68dbd{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-5161523c1b8041f37473c6bfd0a68dbd .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-5161523c1b8041f37473c6bfd0a68dbd .av-subheading{
font-size:14px;
}
</style>
<div  class='av-special-heading av-av_heading-5161523c1b8041f37473c6bfd0a68dbd av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES6: Enhanced Object Literals</h1><div class='av-subheading av-subheading_below'><p>4min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-jss73s39-39503979b53a0a9bcd82e498634a02ee '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>ES6 brings us a better version of object literals. We will write less code to reach the same result.</p>
<h2>No repetition: key-value</h2>
<p>Sometimes we want to create objects on the fly with dynamic values. We can use functions to achieve this by passing them arguments and return objects from them. The object-factory-function needs to initialize the objects with properties. It is quite common to use the same placeholder-name for the key : value pair in such a case. The enhanced object literals in ES6 allow us to just write the placeholdername as the initialized property on the object if key and value pair are the same.</p>
<h3>ES5</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/0b92c758c1e1f989249df17037ecfe88">Gist</a>.</p>
<h3>ES6</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/205d5562bd9134757b3cff45c3606524">Gist</a>.</p>
<h2>No repetition: methods</h2>
<p>Not only key-value pairs can be written short but also methods receive a more concise syntax update. Instead of writing&#8230;</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/6081bfbb43c64382bdccee432d9a84e3">Gist</a>.</p>
<p>&#8230;we can shorten it to:</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/b418edfc0777cd2ff88b6e12b2503c90">Gist</a>.</p>
<h3>ES5</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/2b3d18d626a761a8e9f5046de7858c96">Gist</a>.</p>
<h3>ES6</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/bc456504616606e9eef013069936fbf8">Gist</a>.</p>
<h2>Use variables as keys</h2>
<div>
<div>In ES5 it was not possible to use a variable for a key except it was added afterwards like in the following example:</div>
<h3>ES5</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/1e76dfba127e425c0ef98caa890c8372">Gist</a>.</p>
<h3>ES6</h3>
<p>We can use the short-syntax for writing key-value pairs except for the dynamic added<em> [key1] which </em>has to be written in the classical way.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/df7acdbdddb816ce8d7e42f662d72184">Gist</a>.</p>
<p>Dynamic keys <em>[key1]</em> can be combined with incrementable variables in order to make the keys even more dynamic.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/bd25c67a19d2cb9ddd90c61cf998954e">Gist</a>.</p>
</div>
</div></section><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756">
#top .hr.hr-invisible.av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756{
height:50px;
}
</style>
<div  class='hr av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756 hr-invisible  avia-builder-el-3  el_after_av_textblock  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-8393m-fffacd921dc9ef027db8eab1c632e5c4'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es6-enhanced-object-literals/">ES6: Enhanced Object Literals</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ES6: Descructuring</title>
		<link>https://www.ilearnjavascript.com/es6-descructuring/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Sat, 02 Mar 2019 18:28:54 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=623</guid>

					<description><![CDATA[<p>With ES6 we get a convenient way of extracting multiple values from data stored in (possibly nested) objects and Arrays.</p>
<p>The post <a href="https://www.ilearnjavascript.com/es6-descructuring/">ES6: Descructuring</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975">
.flex_column.av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975 av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-1781cb5eecc0c5c73ac7e3a9b32b8dba">
#top .av-special-heading.av-av_heading-1781cb5eecc0c5c73ac7e3a9b32b8dba{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-1781cb5eecc0c5c73ac7e3a9b32b8dba .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-1781cb5eecc0c5c73ac7e3a9b32b8dba .av-subheading{
font-size:14px;
}
</style>
<div  class='av-special-heading av-av_heading-1781cb5eecc0c5c73ac7e3a9b32b8dba av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES6: Descructuring</h1><div class='av-subheading av-subheading_below'><p>8min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-jsrtlage-dab38bbad308c49e81ee307ceaa1cf0a '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>With ES6 we get a convenient way of extracting multiple values from data stored in (possibly nested) objects and Arrays.</p>
<h3>ES5</h3>
<p>If we wanted to extract single values from an array in ES5 we would manually go through it by its index.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/c17076fa6e01479078a2c04df23c3d2a">Gist</a>.</p>
<h3>ES6</h3>
<p>In ES6 we can simply sum up all variables in an array, give them appropriate names and reference it to the array we want to destruct.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/3a22a810db199a4a59b0205fe6f347d1">Gist</a>.</p>
<h2>Skip values</h2>
<p>If we want to skip some values we just leave them out, but comma separate it that the length of both arrays will still match.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/a0449f6d87601e8509487fe3edff1ea1">Gist</a>.</p>
<h2>Rest Paramter + Destructuring</h2>
<p>Another ES6 feature which comes really handy in combination with destructuring is the <a href="https://www.ilearnjavascript.com/es6-rest-parameter/">Rest Parameter</a>. It is a sibling of the <a href="https://www.ilearnjavascript.com/es6-spread-operator/">Spread Operator</a> (they share the same syntax) and allows us to collect all the remaining elements into an array.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/402da9b8e3996a7600dcdcad70ad2f69">Gist</a>.</p>
<h2>Destructure from Functions</h2>
<p>We can destructure not only directly from an array but also from a function.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/c1e2b7157cdeab17f1089eca77b9d997">Gist</a>.</p>
<h2>Default Parameters + Destructuring</h2>
<p>By using <a href="https://www.ilearnjavascript.com/default-parameters/">Default Paramters</a> we can assign default values in case our array has some <em>undefined</em> values. This could be for example the case when we retrieve the array via an API and cannot assure data reliability but need values to work with.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/9960450dfd46681b69a1ab658641c7d7">Gist</a>.</p>
<h2>Swap values</h2>
<p>A feature of destructuring that is not always easy to find real world applications for is swapping values. So in our case of the person object we can switch the values for the variables of firstname and lastname in one line.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/26a3fdcdf986ef48b90bcbf728ba6014">Gist</a>.</p>
<p>Obviously this example doesn&#8217;t make a lot of sense because why would we switch firstname and lastname. I couldn&#8217;t come up with my own example that makes sense but I found a very good example on github: https://github.com/wesbos/es6-articles/blob/master/20%20-%20Swapping%20Variables%20with%20Destructuring.md</p>
<p>The example in a nutshell: We have a wrestling application in which the tag team partner always switch when they go into the ring. Without destructuring that problem would have been fixed with a temporare variable. Now we can use an easy one-liner.</p>
<h2>Destructuring objects</h2>
<p>Last but not least: Destructuring works also with objects.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/371306e8e8ca1050578f0717e22f4c2d">Gist</a>.</p>
</div></section><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756">
#top .hr.hr-invisible.av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756{
height:50px;
}
</style>
<div  class='hr av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756 hr-invisible  avia-builder-el-3  el_after_av_textblock  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-82e6n-786618bb4597a67cf91ef83c54314d54'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es6-descructuring/">ES6: Descructuring</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ES6: Promises</title>
		<link>https://www.ilearnjavascript.com/es6-promises/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Wed, 27 Feb 2019 17:21:03 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=424</guid>

					<description><![CDATA[<p>Javascript Promises arrived natively with ES6 and are essentially a simpler way of dealing with asynchronous (async) operations in comparison to traditional callback-based approaches (ES5).</p>
<p>The post <a href="https://www.ilearnjavascript.com/es6-promises/">ES6: Promises</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975">
.flex_column.av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975 av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-b2727471536c9c28fce48532cb90f334">
#top .av-special-heading.av-av_heading-b2727471536c9c28fce48532cb90f334{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-b2727471536c9c28fce48532cb90f334 .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-b2727471536c9c28fce48532cb90f334 .av-subheading{
font-size:14px;
}
</style>
<div  class='av-special-heading av-av_heading-b2727471536c9c28fce48532cb90f334 av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES6: Promises</h1><div class='av-subheading av-subheading_below'><p>10min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-jsngzkqe-4712b41a3518dc1ee970bcde40566071 '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>ES6 Promises &#8211; I will promise you love them.</p>
<p>Javascript Promises arrived natively with ES6 and are essentially a simpler way of dealing with asynchronous (async) operations in comparison to traditional callback-based approaches (ES5). They also provide us with a solid error handling simliar to try/ catch and bring a few helpful methods to manage multiple Promises simultaneously.</p>
<p>Typical async operations which Promises can be used for include API calls, DB queries aswell as reading and downloading files.</p>
<p>An ES6 Promise much like a promise in real life can have three possible states:</p>
<ul>
<li><strong>Pending</strong> &#8211; the promise’s outcome hasn’t yet been determined, because the asynchronous operation that will produce its result hasn’t completed yet.</li>
<li><strong>Fulfilled</strong> &#8211; the asynchronous operation has completed, and the promise has a value.</li>
<li><strong>Rejected</strong> &#8211; the asynchronous operation failed, and the promise will never be fulfilled. In the rejected state, a promise has a reason that indicates why the operation failed.</li>
</ul>
<p>Promises are created via the <em>new</em> keyword and take in a function with two arguments <em>resolve</em> and <em>reject</em>. <em>Resolve</em> and <em>reject</em> are essentially methods which return whatever <em>value</em> we pass it. The Promise itself can be chained with a <em>then() and a catch() </em>method which each take a function with one argument. If the Promise gets fullfilled the <em>then()</em> method gets called and the <em>value</em> of <em>resolve()</em> gets passed to the <em>then()</em> method and can be used for any kind of operation. If the Promise gets rejected the <em>catch()</em> method runs with the <em>value</em> (reason) from <em>rejected()</em>.</p>
<p>Lets see that in action. The setTimeout function represents in this simplified example an async operation like an API call, which returns successfull after 2 seconds (fullfilled) and calls the <em>resolve()</em> method with the <em>value &#8216;Win&#8217;.</em> <em>&#8216;Win&#8217;</em> gets passed to the <em>then()</em> method and <em>console.log()</em> outputs it<em> </em>to the console.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/e58ce709abfb5063efa19c80214aac69">Gist</a>.</p>
<h2>Chaining Promises</h2>
<p>Promises can be chained to one another via the <em>then()</em> method and subsequently pass the previously processed value to the next Promise in chain. This works only because both the<em> then()</em> and the <em>catch()</em> method return itself a new Promise.</p>
<p>Lets create a celebrate function which gets called after the sprinter Promise successfully resolved. It takes one argument (the <em>value state</em> which is returned from the sprinter Promise) and returns a resolved Promise with the value of the variable <em>msg</em>. Now we chain to the resolved Promise from the celebrate function an anonymus function that simply logs <em>msg</em>.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/725565a144512930b2d65721c6aa786a">Gist</a>.</p>
<h2>Handling Multiple Promises</h2>
<p>Promises have additionally to<em> resolve()</em> and <em>reject()</em> two more built-in methods <em>all()</em> and<em> race()</em>.</p>
<h3>Promise.all()</h3>
<p>The Promise.all() method takes in an array of multiple promisses and only resolves when all promises are fullfilled. Lets create three different sprinters, which take each a different time to resolve (3 seconds, 2seconds, 1 seconds). The <em>Promise.all()</em> method fires only after 3s when alle Promises are fullfilled.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/a4119ad5885bfcd23236c6658d4b0049">Gist</a>.</p>
<h3>Promise.race()</h3>
<p>The <em>Promise.race()</em> method also takes in an array of multiple promisses and will resolve as soon as the first promise fullfills. The <em>Promise.race()</em> method called on the same three sprinters from the previous example this time will resolve already after 1 seconds (the fastest), because this is when the first sprinter (sprinter3) finishes.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/524dcc36ad870e02b2d2f560c4b4015b">Gist</a>.</p>
<h2>Promises are async</h2>
<p>Promises are not only used for async operations but they are async themselves. Meaning: Any synchronous code which is run before or after the Promise will run first before the Promise resolves or rejects. Lets prove it by creating a very simple Promise that resolves without any delay and console.log(&#8216;middle&#8217;). Before the Promise we console.log(&#8216;start&#8217;) and after the Promise we will console.log(&#8216;end&#8217;).</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/5110c71c4e929c197c2a13a591154c0a">Gist</a>.</p>
<p>Both the &#8216;start&#8217; and &#8216;end&#8217; console.log() statements get evaluated first because they run synchronous opposed to the async Promise.</p>
<h2>Fetch and Promises</h2>
<p>Most developers probably already used Promises without being aware of it. The Fetch API, a simple way to make network requests (similar to XMLHttpRequest or jQuery&#8217;s Ajax), infact returns a Promise.</p>
<p>In the following example we make an API call to a random joke API via fetch which resolves when the server responds with a status somewhere within the 2xx range and provides us with the joke in JSON format. If the Promise gets resolved the <em>then()</em> method gets called and the json is being parsed to an object. Since the json() method returns a Promise itself we can chain it with another <em>then()</em> and log the joke to the console.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/6b8f8d2dff2c29b38845e62ce9054266">Gist</a>.</p>
<h2></h2>
</div></section><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756">
#top .hr.hr-invisible.av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756{
height:50px;
}
</style>
<div  class='hr av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756 hr-invisible  avia-builder-el-3  el_after_av_textblock  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-7dm20-079f3e7babc347d18bc7a7d7ce0b6310'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es6-promises/">ES6: Promises</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ES6: New Methods</title>
		<link>https://www.ilearnjavascript.com/es6-new-methods/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Tue, 26 Feb 2019 19:08:31 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=383</guid>

					<description><![CDATA[<p>With ES6 developers have new built-in methods to make it easier working with Strings, Arrays, Objects and Numbers.</p>
<p>The post <a href="https://www.ilearnjavascript.com/es6-new-methods/">ES6: New Methods</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975">
.flex_column.av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jsm56n9k-b89ad28a2e87aea11f86249f3e974975 av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-dd81f055c592a65dc8c4550254980f30">
#top .av-special-heading.av-av_heading-dd81f055c592a65dc8c4550254980f30{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-dd81f055c592a65dc8c4550254980f30 .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-dd81f055c592a65dc8c4550254980f30 .av-subheading{
font-size:14px;
}
</style>
<div  class='av-special-heading av-av_heading-dd81f055c592a65dc8c4550254980f30 av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES6: New Methods</h1><div class='av-subheading av-subheading_below'><p>15min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-deou7-8f844f4c021aeb681503c15631b9a4ca '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>Developers have now with ES6 new built-in methods to make it easier working with Strings, Arrays, Objects and Numbers.</p>
<h2>String Methods</h2>
<h3>Three methods to search in Strings</h3>
<p>String <strong>starts</strong> with a specific substring.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/f3790d7567a1b0db24ece55f6c6cca0d">Gist</a>.</p>
<p>String <strong>ends</strong> with a specific substring.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/dad1d221c4a51db5a35a07c8d4f07e98">Gist</a>.</p>
<p>String <strong>includes</strong> a specific substring.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/239b304e1d531c6b2771c96a7a4885ef">Gist</a>.</p>
<h3>Repeat a String</h3>
<p>We can repeat a String now very easy with the built-in repeat method.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/d46adcf3b84642ef9db10ab555e0d1d4">Gist</a>.</p>
<h2>Array Methods</h2>
<h3>Included</h3>
<p>Use <em>.includes()</em> to check if a certain value is included in an array.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/031a9f7a4cc806e7704e8d47ea808e52">Gist</a>.</p>
<h3>Find</h3>
<p>Use <em>.find()</em> to look out for a specific value in an array.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/c21942306b4d8d891ebf70f10a47c98d">Gist</a>.</p>
<p>But unlike the filter method it will only find the first match.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/9e72043bfdf23037b65e9275010caab8">Gist</a>.</p>
<h3>Findindex</h3>
<p>Use <em>.findIndex()</em> to determine at which index a specific value is located in an array.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/472b992e9b855772c3b55571e1af3ca6">Gist</a>.</p>
<h3>Fill</h3>
<p>The built-in fill method allows us to fill an array with values. By applying the appropriate parameters we can choose to fill the array with a specific value from and until a specific position.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/a3baecb88887e16c1d412c21b530a995">Gist</a>.</p>
<h3>Values</h3>
<p>This new built-in method returns an array iterator of the values so we can run a for loop to extract each value of the array.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/dc621deac73a22668e76faf33e09a666">Gist</a>.</p>
<h3>Entries</h3>
<p>Array.entries() also returns an iterator but when looped through it returns both the key and value in an array format.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/54ef6d40529d3ccba6b40986f1b6896c">Gist</a>.</p>
<h3>Keys</h3>
<p>Array.keys() returns an iterator</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/023267ba0a97b4984fc80a3915b1971e">Gist</a>.</p>
<h3>Convert to Array</h3>
<p>Convert strings or array-like objects to an array.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/54c664df941ea7368ddac807de5b6925">Gist</a>.</p>
<p>See also my article about the spread operator to learn how to convert an array-like object to an array.</p>
<h2>Objects</h2>
<h3>Merge with Object.assign</h3>
<p>The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. If the properties of target and source object are identical the properties from the taget object get overwritten. Finally it will return the target object.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/e85503d314e322702d1bf70ae83f34c8">Gist</a>.</p>
<p>It can be also used to merge multiple objects into one new object.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/c6459054d7559f0966257a19765301c6">Gist</a>.</p>
<h3>Clone with Object.assign</h3>
<p>As seen before it can be used to clone an object by providing as the target simply an empty object.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/c899b4b48048bd98817fb3e2ccb992bd">Gist</a>.</p>
<p>But be aware for deep cloning, we need to use other alternatives because Object.assign() copies property values. If the source value is a reference to an object, it only copies that reference value.</p>
<h3>Get Object properties as array with Object.entries</h3>
<p>The Object.entries() method returns an array of a given object&#8217;s own enumerable property [key, value] pairs.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/0e46a0633268b613ff3955261f710a9d">Gist</a>.</p>
<h4 id="d477" class="graf graf--h4 graf-after--figure">Get property information with Object.getOwnPropertyDescriptors</h4>
<p>Sometimes we need more insights about our objects own properties.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/8156e56926d459209ed9ef9380d0abcb">Gist</a>.</p>
<h2>Numbers</h2>
<h3 id="bb0f" class="graf graf--h4 graf-after--p">Number.isNaN()</h3>
<p>Number.isNaN() checks if the value passed in is NaN or not.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/3bca07465e19b9677df63f6bddf2d27e">Gist</a>.</p>
<h3 id="4091" class="graf graf--h4 graf-after--figure">Number.isInteger()</h3>
<p>Number.isInteger() checks if the value passed in is an integer or not.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/0de67433ec86f55e23ce3e8d5b2a578b">Gist</a>.</p>
<h3 id="b5dc" class="graf graf--h4 graf-after--figure">Number.isSafeInteger()</h3>
<p>Number.isSafeInteger() checks if the value passed in is a safe number. This feature is useful in validating user input.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/330517710e6171f64009813638303121">Gist</a>.</p>
<h3>Number.isFinite()</h3>
<p>Number.isFinite() checks if the value passed in is a finite number or not.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/ea37348f1c14eb68a97a3a6912ae8b32">Gist</a>.</p>
</div></section><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756">
#top .hr.hr-invisible.av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756{
height:50px;
}
</style>
<div  class='hr av-jsm56dz6-c6cf5812234ac3bae2a3b34780952756 hr-invisible  avia-builder-el-3  el_after_av_textblock  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-8mskf-48b5676217aa2e3c8104784bc7d96f47'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es6-new-methods/">ES6: New Methods</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ES6: Spread Operator</title>
		<link>https://www.ilearnjavascript.com/es6-spread-operator/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Tue, 26 Feb 2019 16:26:03 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=358</guid>

					<description><![CDATA[<p>The spread syntax allows arrays, objects and array like objects (e.g. arguments object, DOM node objects) to be expanded in places where multiple arguments (for function calls) are expected.</p>
<p>The post <a href="https://www.ilearnjavascript.com/es6-spread-operator/">ES6: Spread Operator</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jslzfiab-97362e357bc6ee6c92e708ad014cc46c">
.flex_column.av-jslzfiab-97362e357bc6ee6c92e708ad014cc46c{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jslzfiab-97362e357bc6ee6c92e708ad014cc46c av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-375003bbca43a8203e0505be666c74be">
#top .av-special-heading.av-av_heading-375003bbca43a8203e0505be666c74be{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-375003bbca43a8203e0505be666c74be .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-375003bbca43a8203e0505be666c74be .av-subheading{
font-size:15px;
}
</style>
<div  class='av-special-heading av-av_heading-375003bbca43a8203e0505be666c74be av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES6: Spread Operator</h1><div class='av-subheading av-subheading_below'><p>6min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-na19a-23ef496c4c636819d9c8389669e51341 '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>The spread syntax allows arrays, objects and array like objects (e.g. arguments object, DOM node objects) to be expanded in places where multiple arguments (for function calls) are expected. In order to use the spread operator simply write three dots &#8220;&#8230;&#8221; preceeding the expression that should get &#8220;spread&#8221;.</p>
<p>The Spread Operator is a sibling of the <a href="https://www.ilearnjavascript.com/es6-rest-parameter/">Rest Parameter</a> which both share the same syntax. It is a great new feature and probably best observed in action.</p>
<h2>The better apply</h2>
<p>We can use the spread operator instead of apply to call a function and pass it an array of arguments.</p>
<h3>ES5</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/220c25fcf67c1616bacc13b18946d358">Gist</a>.</p>
<h3>ES6</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/e339863c56889b7d25fa6f331154ca63">Gist</a>.</p>
<h2>Merging arrays</h2>
<p>We can use the spread operator to merge multiple arrays.</p>
<h3>ES5</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/54a54c22ce43b6e2e9c96d3037427220">Gist</a>.</p>
<h3>ES6</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/e4fd02e2f50dc8448b246960f0bf9bc5">Gist</a>.</p>
<p>The array can be spreaded anywhere it is necessary.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/69e674844be8aab52d19ad6c0e0bd7aa">Gist</a>.</p>
<p>As initially stated it works not only for arrays and array-like objects but also objects itself. We can use the spread operator to create a fresh new copy of an object that inherits all the properties of the cloned object. But be aware: the spread operator is not suitable to deep clone an object as it goes only one level deep.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/3f9c2bde1005edeae983abf9f5ae35db">Gist</a>.</p>
<h2>Copy an array</h2>
<p>We can use it to make a frew new copy of an array without a reference.</p>
<h3>ES5</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/fb4060f4b9ff17750c42994b1ecbfc21">Gist</a>.</p>
<h3>ES6</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/e79208810e8c5eb9708efe0a2a8e433b">Gist</a>.</p>
<h2>Convert array-like objects to arrays</h2>
<p>Sometimes it is necessary to convert an array-like object to an array, e.g. in order to use methods which are only available for arrays such as filter, map or reduce.</p>
<h3>ES5</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/4a9c747b1245927bedbd40ae1ca5e643">Gist</a>.</p>
<h3>ES6</h3>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/a41638a60320946c8061443e2769c4c3">Gist</a>.</p>
</div></section><br />
<div  class='hr av-e8by6-75267cc728f96f299aa3758d57a401e3 hr-default  avia-builder-el-3  el_after_av_textblock  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-9lk3a-2b245f3d21f908a69617574492282475'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es6-spread-operator/">ES6: Spread Operator</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ES6: Arrow functions</title>
		<link>https://www.ilearnjavascript.com/es6-arrow-functions/</link>
		
		<dc:creator><![CDATA[ILJS]]></dc:creator>
		<pubDate>Tue, 26 Feb 2019 13:44:02 +0000</pubDate>
				<category><![CDATA[ES6+]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[es6+]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.ilearnjavascript.com/?p=336</guid>

					<description><![CDATA[<p>Arrow functions are a simpler and more concise way to declare functions and inherit the feature to solve the "this" problem in javascript.</p>
<p>The post <a href="https://www.ilearnjavascript.com/es6-arrow-functions/">ES6: Arrow functions</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsltmkcb-a5c7352c399fc6e6abaa590cf885f7ae">
.flex_column.av-jsltmkcb-a5c7352c399fc6e6abaa590cf885f7ae{
border-radius:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
}
</style>
<div  class='flex_column av-jsltmkcb-a5c7352c399fc6e6abaa590cf885f7ae av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  blogpost first flex_column_div av-zero-column-padding  '     ><p>
<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-av_heading-7a219c2dbf8830c91ac91c5ebd1a11d5">
#top .av-special-heading.av-av_heading-7a219c2dbf8830c91ac91c5ebd1a11d5{
padding-bottom:10px;
}
body .av-special-heading.av-av_heading-7a219c2dbf8830c91ac91c5ebd1a11d5 .av-special-heading-tag .heading-char{
font-size:25px;
}
.av-special-heading.av-av_heading-7a219c2dbf8830c91ac91c5ebd1a11d5 .av-subheading{
font-size:14px;
}
</style>
<div  class='av-special-heading av-av_heading-7a219c2dbf8830c91ac91c5ebd1a11d5 av-special-heading-h1 blockquote modern-quote  avia-builder-el-1  el_before_av_textblock  avia-builder-el-first '><h1 class='av-special-heading-tag '  itemprop="headline"  >ES6: Arrow functions</h1><div class='av-subheading av-subheading_below'><p>10min read</p>
</div><div class="special-heading-border"><div class="special-heading-inner-border"></div></div></div><br />
<section  class='av_textblock_section av-jslts3nw-349a9447968c4b72f76d591b4c5dbb3b '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><p>Arrow functions are a simpler and more concise way to declare functions which also inherit the feature to solve the &#8220;<em>this</em>&#8221; problem in javascript.</p>
<h2>Arrow functions in a nutshell</h2>
<p>Arrow functions allow us to write Functions without the <em>function</em> keyword and possibly even in one line. They also fix the problem with the <em>this</em> keyword in Javascript but more on this topic later.</p>
<h3>ES5</h3>
<p>Instead of writing the <em>function</em> keyword explicitely&#8230;</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/90d408dfbd6f5b4f1832bc482b7245c9">Gist</a>.</p>
<h3>ES6</h3>
<p>&#8230;we can now just simply leave it out and write a &#8220;<em>=&gt;&#8221; (fat arrow)</em> after the parameter(s).</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/5a97910e1c0d7849f537899ea884e594">Gist</a>.</p>
<p>Too shorten it even more we can wrap the code also on one line with the return statement implicitely added.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/37161d4b66a877543f07064599cc1d16">Gist</a>.</p>
<p>If there is more than one parameter present we need to wrap them both in Parenthesis before the<em> &#8220;</em>=&gt;&#8221;<em> (fat arrow)</em>.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/28586c6d2d469e8b21e1f50d2c7265b7">Gist</a>.</p>
<h2>Neat code</h2>
<p>I personally like how code with fat arrow functions looks like. It appears more neat to my &#8220;developers eye&#8221;. But judge for yourself:</p>
<h3>ES5</h3>
<p>This function written in ES5 syntax&#8230;</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/7b0e450101fa696789e6ebf43a69992f">Gist</a>.</p>
<h3>ES6</h3>
<p>&#8230;would look like this in ES6 syntax.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/a4cab48b299b72b0c9295e922f4c86d2">Gist</a>.</p>
<h2>The <em>this</em> problem</h2>
<p>In order to understand how fat arrow functions solve the <em>this</em> problem lets recap first what it actually means.</p>
<p>When we declare a method on an object, the this keyword of that method points to the object itself.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/d2f876c80f39597e61dc25ce51fe8afe">Gist</a>.</p>
<p>The problem is what happens when we define another function within the object&#8217;s method that makes also use of the <em>this</em> keyword and run it. While the <em>this</em> of the outerMethod still points to the object itself, the <em>this</em> of the innerMethod loses its context and points to the global object (<em>window</em>).</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/37e9d21bdd1a7c72a22e1f31b3b79e1e">Gist</a>.</p>
<h3>Old ways (ES5) to fix it</h3>
<p>In &#8220;the old days&#8221; we could fix that problem in multiple ways.</p>
<p>1. We declare in the outerMethod a new <em>var _this</em> and set it equal to <em>this </em>(reference it). From that point on we can use it as wanted in nested functions.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/c8fcd1004aadc4d41f9abe09699e8312">Gist</a>.</p>
<p>2. We transfer the <em>this</em> context of the outerMethod to the innerMethod by using <em>bind(this) </em>on the innerMethod.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/e7a4c2c41675b100654bfc764718cf87">Gist</a>.</p>
<p>3. We transfer the <em>this</em> context from the outerMethod to the innerMethod by envoking the innerMethod with call<em>(this) </em>or<em> apply(this)</em></p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/3fe52df9eee9d4114df8293a211e55e8">Gist</a>.</p>
<h3>Fat arrow to the rescue</h3>
<p>Fat arrow functions don&#8217;t have their own <em>this</em> context but instead inherit it from &#8220;where&#8221; they are defined in.</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/778a6beb1f2c59bbff71f65d5b310e7a">Gist</a>.</p>
</div></section><br />
<section  class='av_textblock_section av-jslx2wi0-39dfd4a6f84ef9eef0a4b49ce78e66a5 '   itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div class='avia_textblock'  itemprop="text" ><h2>Quiz</h2>
<p>Do you think you understood fat arrow functions? Test your knowledege with this small quiz.</p>
<h3>Question:</h3>
<p>What would be the value of <em>this</em> for both the inner and outer method if we defined the outerMethod also as a fat arrow function?</p>
<p>View the code on <a href="https://gist.github.com/ilearnjavascript/7854e48a6efea9379d3cb62533d78aa2">Gist</a>.</p>
</div></section><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jslx50d8-0205b37110a269f042bd5d86191c6712">
#top .togglecontainer.av-jslx50d8-0205b37110a269f042bd5d86191c6712 p.toggler{
color:#000000;
background-color:#f9de58;
border-color:#f9de58;
}
#top .togglecontainer.av-jslx50d8-0205b37110a269f042bd5d86191c6712 p.toggler .toggle_icon{
color:#000000;
border-color:#000000;
}
#top .togglecontainer.av-jslx50d8-0205b37110a269f042bd5d86191c6712 .toggle_wrap .toggle_content{
color:#000000;
background-color:#f9de58;
border-color:#f9de58;
}
</style>
<div  class='togglecontainer av-jslx50d8-0205b37110a269f042bd5d86191c6712 av-elegant-toggle  avia-builder-el-4  el_after_av_textblock  el_before_av_hr  toggle_close_all' >
<section class='av_toggle_section av-d55fs-48b75614025e415adb543c6a101a5edf'  itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost" ><div role="tablist" class="single_toggle" data-tags="{All} "  ><p id='toggle-toggle-id-1' data-fake-id='#toggle-id-1' class='toggler  av-title-above av-inherit-font-color hasCustomColor av-inherit-border-color'  itemprop="headline"  role='tab' tabindex='0' aria-controls='toggle-id-1' data-slide-speed="200" data-title="Click to show result" data-title-open="" data-aria_collapsed="Click to expand: Click to show result" data-aria_expanded="Click to collapse: Click to show result">Click to show result<span class="toggle_icon"><span class="vert_icon"></span><span class="hor_icon"></span></span></p><div id='toggle-id-1' aria-labelledby='toggle-toggle-id-1' role='region' class='toggle_wrap  av-title-above'  ><div class='toggle_content invers-color av-inherit-font-color hasCustomColor av-inherit-border-color'  itemprop="text" ><p><strong>Result:</strong></p>
<p>outer this Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}</p>
<p>inner this Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}</p>

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-bqb6w-1ece9804e2e92e92c6c62a57bd566d35">
#top .hr.hr-invisible.av-bqb6w-1ece9804e2e92e92c6c62a57bd566d35{
height:20px;
}
</style>
<div  class='hr av-bqb6w-1ece9804e2e92e92c6c62a57bd566d35 hr-invisible  avia-builder-el-5  avia-builder-el-no-sibling '><span class='hr-inner '><span class="hr-inner-style"></span></span></div>
<p>Since the context of the outerMethod is the global <em>window</em> object, the <em>this</em> points to the <em>window</em>.</p>
</div></div></div></section>
</div><br />

<style type="text/css" data-created_by="avia_inline_auto" id="style-css-av-jsltmxh4-91cea0820d5952610f1af487dd979041">
#top .hr.hr-invisible.av-jsltmxh4-91cea0820d5952610f1af487dd979041{
height:50px;
}
</style>
<div  class='hr av-jsltmxh4-91cea0820d5952610f1af487dd979041 hr-invisible  avia-builder-el-6  el_after_av_toggle_container  el_before_av_comments_list '><span class='hr-inner '><span class="hr-inner-style"></span></span></div><br />
<div  class='av-buildercomment av-afneo-ebc64b2e700c5fc470979f8b906b7500'></div></p></div>
<p>The post <a href="https://www.ilearnjavascript.com/es6-arrow-functions/">ES6: Arrow functions</a> appeared first on <a href="https://www.ilearnjavascript.com">I Learn Javascript</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
