{"id":112,"date":"2018-07-03T21:49:20","date_gmt":"2018-07-03T21:49:20","guid":{"rendered":"https:\/\/www.mohammedlakkadshaw.com\/blog\/?p=112"},"modified":"2018-07-03T21:49:20","modified_gmt":"2018-07-03T21:49:20","slug":"most-useful-es6-features","status":"publish","type":"post","link":"https:\/\/mohammedlakkadshaw.com\/blog\/most-useful-es6-features.html","title":{"rendered":"5 Most useful ES6+ Features"},"content":{"rendered":"<p>In this post, I am going to share with you Javascript ES6+ features that I find the most useful:<\/p>\n<ol>\n<li>For&#8230;of Loop<\/li>\n<li>Arrow Functions<\/li>\n<li>Async and Await<\/li>\n<li>Template literals<\/li>\n<li>Destructing<\/li>\n<\/ol>\n<h3>For&#8230;of Loop<\/h3>\n<p>Before either we had to write a full for loop or call the forEach function on the array to iterate over its item.<\/p>\n<p>Previously\u00a0we had to do this<\/p>\n<pre class=\"lang:default decode:true \">var animals = ['cat', 'dog', 'monkey', 'goat'];\nanimals.forEach(function(animal) {\n    console.log(animal);\n});\n\n\/\/ OR THIS\n\nfor(var i=0; i &lt; animals.length; i++) {\n    console.log(animals[i])\n}<\/pre>\n<p>Now with the new version of JavaScript, we can simply write the For&#8230;Of loop<\/p>\n<pre class=\"lang:default decode:true \">for (let animal of animals) {\n    console.log(animals)\n}<\/pre>\n<p>If we have to iterate over an object&#8217;s keys we can use the For&#8230;In loop<\/p>\n<pre class=\"lang:default decode:true \">var address = {\n    street: '123 Example Street',\n    city: 'TestCity',\n    state: 'CoolState'\n};\n\nfor (let key in address) {\n    console.log(address[key]);\n}<\/pre>\n<h2>Arrow Functions<\/h2>\n<p>If you have done programming in JavaScript you must be familiar that we have to create a LOT of functions in javascript as it is an asynchronous programming language.<\/p>\n<p>With the new Arrow Functions, it makes the code cleaner and also saves you a few keystrokes.<\/p>\n<p>Consider the following example, previously\u00a0we had to do this<\/p>\n<pre class=\"lang:default decode:true \">setTimeout(function() {\n    console.log('5 seconds later')\n}, 5000)<\/pre>\n<p>Now with the ES6+ syntax, it can be compressed into a single line, in a single like arrow function we don&#8217;t have to write the return statement, the value of whatever expression preceding the arrow will be returned.<\/p>\n<pre class=\"lang:default decode:true \">setTimeout(() =&gt; console.log('5 seconds later'), 5000)<\/pre>\n<p>We can also create multi-line arrow functions that accept multiple arguments<\/p>\n<pre class=\"lang:default decode:true\">const compute = (arg1, arg2, arg3) =&gt; {\n    const total = arg1 + arg2 + arg3;\n    return total * total;\n}\n\ncompute(4,5,6)<\/pre>\n<h2>Async and Await<\/h2>\n<p>This is the feature that I use a lot especially\u00a0when doing server-side\u00a0programming using Node.JS, but you can also use it on the front-end and it also works in all the major browsers.<\/p>\n<p>With Async and Await keywords, you can write asynchronous code that uses promises appear as if it is synchronous and make the code more readable.<\/p>\n<p>Consider the following example without async await<\/p>\n<pre class=\"lang:default decode:true \">function doSomething() {\n    getData().then((data)=&gt; { \/\/asychronous call\n        const dataWithSomething = doSomethingWith(data) \/\/sychronous call\n        passDataToAnotherService(dataWithSomething).then((processedData) =&gt; { \/\/asychronous call\n            doSomeMoreThing(processedData) \/\/sychronous call\n        })\n    })\n}<\/pre>\n<p>Now with Async and Await keywords, we can update the code and make the asynchronous functions look like synchronous functions and it results in a much cleaner code, see for yourself:<\/p>\n<pre class=\"lang:default decode:true \">async function doSomething() {\n    const data = await getData();\n    const dataWithSomething = await doSomethingWith(data);\n    const processedData = await passDataToAnotherService(dataWithSomething);\n    doSomeMoreThing(processedData)\n}<\/pre>\n<p>As you can see with the update the code is much more simplified. To use async-await first you have to create a function with keyword async, then inside the function, you can call asynchronous methods that uses\u00a0promise and add the await keyword in front of the function call.<\/p>\n<h2>Template literals<\/h2>\n<p>Template literals is a very useful feature, previously we had to use string concatenation the can quickly get out of hand if we are using multiple values or if want to construct a multi-line string, consider the following example<\/p>\n<pre class=\"lang:default decode:true \">var bookCount = 100;\nvar message = 'Your have ' + bookCount + ' books.' \n<\/pre>\n<p>The code can be simplified with template literals<\/p>\n<pre class=\"lang:default decode:true \">var books = 100;\nvar message = `You have ${bookCount} books.`;<\/pre>\n<p>To use the template literal we have to enclose the string in ` and then enclose the variable name under ${&lt;variable_name&gt;}<\/p>\n<p>Template literals can also expand to multiple lines<\/p>\n<pre class=\"lang:default decode:true \">var message = `\n    You have ${bookCount} books in your library.\n    The most popular title in you collection is: ${popularTitle}.\n    Your have favourited ${favBookCount} books.\n`<\/pre>\n<p>Imagine constructing the above code using string concatenation, it would be an unreadable\u00a0mess.<\/p>\n<h2>Destructuring<\/h2>\n<p>Destructuring allows us to extract values from an object and easily assign\u00a0it to variables.<\/p>\n<p>Considering the following example, here is what we normally do when we want to extract values from an object<\/p>\n<pre class=\"lang:default decode:true\">var person = {\n    name: 'James Bond',\n    age: 41,\n    occupation: 'plumber'\n}\n\n\/\/ Without Destructing\nconst name = person.name;\nconst age = person.age;\nconst occupation = person.occupation;\n\n<\/pre>\n<p>With destructuring you can simplify the code as following:<\/p>\n<pre class=\"lang:default decode:true\">\/\/With Destructing\nconst { name, age, occupation } = person;\n\n\/\/ And we can normally use the defined variables\nconsole.log(`The name is ${name}`);\n<\/pre>\n<p>and we can use the name, age, and occupation as variables in our code.<\/p>\n<h2>That&#8217;s it!<\/h2>\n<p>These the feature that I find the most useful in ES6+, please share if you found it interesting\u00a0and let me know in the comments what ES6+ feature you like the best.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I am going to share with you Javascript ES6+ features that I find the most useful: For&#8230;of Loop Arrow Functions Async and Await Template literals Destructing For&#8230;of Loop Before either we had to write a full for loop or call the forEach function on the array to iterate over its item. Previously\u00a0we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,7],"tags":[],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-javascript","category-nodejs"],"_links":{"self":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/comments?post=112"}],"version-history":[{"count":0,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/112\/revisions"}],"wp:attachment":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/categories?post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/tags?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}