
{"id":4694,"date":"2021-07-28T12:00:00","date_gmt":"2021-07-28T10:00:00","guid":{"rendered":"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/uncategorized\/introduction-to-es6-javascript\/"},"modified":"2022-02-07T14:24:06","modified_gmt":"2022-02-07T13:24:06","slug":"introduction-to-es6-javascript","status":"publish","type":"post","link":"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/web-development\/introduction-to-es6-javascript\/","title":{"rendered":"Why JavaScript ES6 and Its Successors Are So Great"},"content":{"rendered":"<p>If you\u2019ve done any <a href=\"\/en\/blog\/web-development\/introduction-to-javascript\/\" data-schema-attribute=\"\">programming in JavaScript<\/a> at all, you probably know what the \u2018var\u2019 keyword does. But how about the \u2018const\u2019 keyword? Maybe you\u2019ve heard of it. Maybe you use it because you know it\u2019s a new feature, but aren\u2019t exactly sure why.<\/p>\n<p>Just like the \u2018var\u2019 keyword, \u2018const\u2019 helps you define variables in JavaScript. However, \u2018const\u2019 is a newish feature and offers more fine-grained control over the behavior of your application\u2019s variables. This new keyword is only one part of a much broader set of new additions to JavaScript that have become available to use with ES6.<\/p>\n<p>In this article, I&#8217;m going to explain just what JavaScript ES6 can do and why you should get excited. In fact if you don&#8217;t already, you&#8217;ll be looking to <a href=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/web-development\/how-long-does-it-take-to-learn-javascript\/\">learn JavaScript as quickly as possible<\/a> in order to get your hands on it.<\/p>\n<p>First we&#8217;re going to look at what ES6 is exactly, and then give some background to it. We&#8217;ll then talk about transpiling, as well as some of the other cool features that this standard brings to the table. Finally, we&#8217;ll share some key resources if you want to go and learn more about ES6, and where to go next.<\/p>\n<p>If you&#8217;d like to skip ahead to one of the sections of the article directly, feel free to use the clickable menu:<\/p>\n<ol>\n<li><a href=\"#what-exactly-is-es6\">What exactly is ES6?<\/a><\/li>\n<li><a href=\"#a-little-history\">A little history<\/a><\/li>\n<li><a href=\"#a-note-about-transpiling\">A note about transpiling<\/a><\/li>\n<li><a href=\"#common-post-es5-javascript-features-you-should-know\">Common post-ES5 JavaScript features you should know<\/a><\/li>\n<li><a href=\"#where-can-i-learn-more-about-es6\">Where can I learn more about ES6?<\/a><\/li>\n<li><a href=\"#wrap-up\">Wrap-up<\/a><\/li>\n<\/ol>\n<h2 id=\"what-exactly-is-es6\">1. What exactly is ES6?<\/h2>\n<p>Technically speaking, ES6 is a standard that was released in 2015. It tells companies that make browsers how exactly they should make JavaScript work in their respective browsers. \u201cBut,\u201d you might ask, \u201cJavaScript is JavaScript\u2026 Why would it work differently across browsers?\u201d\u2026<\/p>\n<h2 id=\"a-little-history\">2. A little history<\/h2>\n<p>Back in the late 1990s, just like today, we had multiple companies making their own versions of browsers. When JavaScript was invented, each company had its own idea about how exactly JavaScript code should act in their respective browsers. Because this kind of inconsistency is unsustainable, ECMA International, a standards organization, came forward and basically unified all the JavaScript implementations across all browsers. ECMA said, \u201cIf you want to make a language that runs on your browser, you should make it behave according to our guidelines so that JavaScript code that a developer writes for one browser will work the same on other browsers.\u201d<\/p>\n<p><em>*Keep in mind, the set of specifications created by ECMA is called \u201cECMAScript\u201d\u2014 and JavaScript is an implementation of it. This is why you may hear people refer to JavaScript as \u201cECMAScript\u201d.<\/em><\/p>\n<p>Over the years, ECMA has been tweaking its guidelines little by little\u2014adding requirements for new features\u2014meaning that companies that make JavaScript engines have to implement those features in order to keep up. Each time ECMA releases a new set of specifications, companies must change their JavaScript implementations\u2014meaning they have to add the new features added by ECMA.<\/p>\n<p>In June of 2015, ECMA released a major new set of specifications that we know as ES6. We also know it as ES2015\u2014the year it was released. In this major release, there were many new requirements added with the intention of bringing JavaScript up to date with modern programming standards.<\/p>\n<p>Since 2015, ECMA has been releasing a new version of its specifications every year. Each June for the past three years, new requirements have continued to be added. So while you\u2019ll hear about ES6 the most (as it brought the biggest changes), there have since been three releases as of the writing of this article: ES2016, ES2017, and ES2018\u2014each adding new requirements for JavaScript features. The current version is <a href=\"https:\/\/www.ecma-international.org\/ecma-262\/9.0\/\" target=\"_blank\" rel=\"noopener\">ECMAScript 2018 Language Specification<\/a>. Take a look if you have the time and see just how much there is to JavaScript.<\/p>\n<h2 id=\"a-note-about-transpiling\">3. A note about transpiling<\/h2>\n<p>What happens if you write some ES6 code for an application and your users want to use it but they haven\u2019t updated to the latest version of the browser that implements ES6? Let\u2019s say your users still use IE8. IE8 does not implement ES6. Will your ES6 code work on their browser? No, it won\u2019t. That\u2019s why we have \u201ctranspilers\u201d. In this case, a transpiler basically takes your ES6 code and converts it to pre-ES6 code (\u201cES5\u201d). It then sends the transpiled JavaScript code so that older versions of browsers that only know ES5 can understand it.<\/p>\n<p>A transpiler called Babel is the de facto standard for transpiling newer JavaScript code to ES5. As a developer, you probably won\u2019t need to run Babel in order to run your own apps because you\u2019re probably using the most up-to-date browsers available. However, if you want to distribute your application written in ES6 to the world, you\u2019ll have to remember that not everyone uses the most up-to-date browsers. You\u2019ll therefore need to use a transpiler to maximize the number of users that can access your application.<\/p>\n<p>In most cases, if you\u2019re using any kind of newer JavaScript\u00a0framework like <a href=\"http:\/\/React.js.\" rel=\"noopener\">React<\/a> or Angular, the transpiler, along with all the code needed to run it, will be included in the \u201cstarter code\u201d. This means that you won\u2019t have to worry about implementing the transpiler yourself because the developers that built the <a href=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/web-development\/javascript-frameworks\/\" target=\"_blank\" rel=\"noopener\">JavaScript frameworks<\/a> and the starter code have already done the work for you. You can just start coding in ES6 without even having to think about transpiling. It\u2019s very convenient.<\/p>\n<p>So without further ado, let\u2019s dive in to some actual ES6 code.<\/p>\n<h2 id=\"common-post-es5-javascript-features-you-should-know\">4. Common post-ES5 JavaScript features you should know<\/h2>\n<p>Here are some of the most common features of ES2015 as well as some others from ES2016 &#8211; ES2018.<\/p>\n<p>Many features that we associate with newer JavaScript functionality nowadays came in with ES6. But there are lots of other features that came in with ES2016 and after. In this section, I\u2019ll show you many common features that you\u2019ll likely be seeing in existing code bases. Keep in mind that not all of these features came in with ES6, but with your transpiler, you\u2019ll be able to use them all because it will convert them back to ES5.<\/p>\n<h3 id=\"keyword-const\">Keyword &#8220;const&#8221;<\/h3>\n<p>Sometimes we want to make a variable that we don\u2019t want to change (AKA a \u201cconstant\u201d)\u2014either by another developer or by ourselves. ES6 gives us access to \u2018const\u2019. When we create a constant, if we ever try to re-assign the value, we\u2019ll get an error. Not only is this useful, but it is becoming more and more common to use \u2018const\u2019 in place of \u2018var\u2019 when we have a variable that we don\u2019t want to change (again, we call it a \u201cconstant\u201d because it is not \u201cvariable\u201d!).<\/p>\n<p>Below, you can see how to create and change variables using the \u2018var\u2019 keyword:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/49fcf7469d5474c5f9b60fe2dfafeaf0.js\"><\/script><\/p>\n<p>Next, we try the same action using \u2018const\u2019:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/0edcd92c2d86291ea0ce2145f1e44a74.js\"><\/script><\/p>\n<p>In the above example, we see that we can create a constant \u201ccatName\u201d but cannot re-assign a value to it. If we try, we get an error. This behavior is what we want! Now no one (including us) can change the value! There\u2019s much more to \u2018const\u2019 than the example above. You can follow the link at the bottom of this article to learn more about \u2018const\u2019.<\/p>\n<h3 id=\"template-literals\">Template literals<\/h3>\n<p>Template literals also help speed up development. They are strings that we define with backticks (`) instead of quotes or double quotes (\u2018 , \u201c). They allow us to use embedded expressions with %{} so we don\u2019t have to concatenate strings and expressions together. They also help us display <a href=\"\/en\/blog\/web-development\/what-is-html-a-beginners-guide\/\">html<\/a> in a format closer to what we actually write in the code. For example, when we start a new line in a template literal, we see the same effect in the html.<\/p>\n<p>In ES5, we have to concatenate (join together) strings with \u2018+\u2019. When we want to concatenate strings with variables, we again have to end the string, add \u2018+\u2019, and start the string again. And to break onto a new line, we need to add \u2018n\u2019 inside our strings to indicate a new line. Take a look at the old way below and notice how many times we have to break up the string:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/c70823665c0bb13ef5467590842fbfe2.js\"><\/script><\/p>\n<p>But in ES6, we use \u201ctemplate literals\u201d. We don\u2019t need to break our strings apart to concatenate variables inside the strings, nor do we need \u2018n\u2019! If we simply wrap our messages in backticks, all this is taken care of automatically. Below, you see that we simply wrap our variables in \u2018${}\u2019 and leave it inside the string. Easy!<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/ab3dfdff4987d9f3ee69bde0238c3933.js\"><\/script><\/p>\n<h3 id=\"br3-arrow-functions\">Arrow functions<\/h3>\n<p>Arrow functions help us write more concise function expressions (among other things). Let\u2019s first take a look at two ways to create functions before ES6:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/c2478e326ebef49d4b683a56f3ff7f8a.js\"><\/script><\/p>\n<p>The first example above (sayHello1) is called a function statement, and the second (sayHello2) is called a function expression. The difference between these two is outside the scope of this article. Just know that they both create functions that can be called by attaching parentheses to their \u201cnames\u201d.<\/p>\n<p>In the next example you\u2019ll see the use of the \u201cfat arrow\u201d added in ES6. These shortened versions are called \u201carrow functions\u201d and are basically a quicker way to write function expressions.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/5dcd7a2dddbaecef7df2ea3571a97fe9.js\"><\/script><\/p>\n<p>Note, in \u2018sayHello3\u2019, we remove the \u201cfunction\u201d keyword before the parentheses. Then we add the \u2018fat arrow\u2019 after the parentheses and can then define the body of the function as normal.<\/p>\n<p>Note, in \u2018sayHello4\u2019, because there\u2019s only one single argument, we can remove the surrounding parentheses. And because the return statement is the only line in the block of code, we don\u2019t need the return keyword and can simply write `Hello ${name}` all on the same line without any parentheses! This really helps make things go quicker once you get used to the syntax!<\/p>\n<h3 id=\"iterators\">Iterators<\/h3>\n<p>\u2018For of\u2019 iterators help us iterate over arrays (among other things). Instead of the old way (a basic \u2018for\u2019 loop), we use \u2018for of\u2019. This ensures we iterate over every element in the array and we don\u2019t have to worry about off-by-one errors that can come from being sloppy with a conventional \u2018for\u2019 loop.<\/p>\n<p>As you probably know, JavaScript gives us \u2018for\u2019 loops to loop through arrays. Here are a couple ways to iterate over arrays:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/5a15d4e32c7c64bff59dccd6732ff8f2.js\"><\/script><\/p>\n<p>And here\u2019s the new and improved ES6 version:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/bee4caa7a5658020752d95b8126dc7f9.js\"><\/script><\/p>\n<p>Notice you use the keyword \u2018let\u2019 to represent the element name as you iterate over the array. The block of code will be run for every element in the array. You can act on each element by manipulating the variable you define after the \u2018let\u2019 keyword above it. In the above example, we just printed the value, but you can do whatever you could do with the original \u2018for\u2019 loop!<\/p>\n<h3 id=\"default-parameters\">Default parameters<\/h3>\n<p>Default parameters allow us to specify the value of a parameter in the off-chance the function caller does not pass in a value.<\/p>\n<p>If you want to make some parameters optional for your functions (meaning the caller can pass a value or not), but your block still depends on the variable being defined, you\u2019d set a \u201cdefault variable\u201d. Here\u2019s how you\u2019d do it in ES5:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/82a97fa4e18c17d9856dc994f858ce51.js\"><\/script><\/p>\n<p>ES6 gives us an easier way to do it right inside the parentheses when we set our parameters:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/5e3ab6c4d9f4e35ba280c046077939bf.js\"><\/script><\/p>\n<h3 id=\"internationalization-features\">Internationalization features<\/h3>\n<p>JavaScript can now format and display currency, time, and numbers for many different countries. Here\u2019s an example of how you could take a number and format it in different currencies.<\/p>\n<p>I\u2019m including this as a bonus feature to show you the fun things you can do nowadays with JavaScript. Here\u2019s an example of how you can take a normal number and tell JavaScript to format it based on options you\u2019d pass in when you define the format:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Bill-Horst\/267b843432747720d40944629a890dac.js\"><\/script><\/p>\n<p>There is no way to do this cool stuff in ES5. The above just shows some fun (and useful) stuff we now have access to thanks to ES6!<\/p>\n<h2 id=\"where-can-i-learn-more-about-es6\">5. Where can I learn more about ES6?<\/h2>\n<p>There are many places you can go to learn more about ES6 and the newer specifications.<\/p>\n<p>If you feel like reading through the actual documentation of ECMAScript, you can find it in <a href=\"https:\/\/www.ecma-international.org\/ecma-262\/6.0\/\" target=\"_blank\" rel=\"noopener\" data-schema-attribute=\"\">the ES6 (ES2015) document on their website<\/a> This is where you would also look for the <a href=\"https:\/\/www.ecma-international.org\/ecma-262\/9.0\/\" target=\"_blank\" rel=\"noopener\" data-schema-attribute=\"\">specification for ES2018<\/a> as well.<\/p>\n<p><a href=\"http:\/\/es6-features.org\/#Constants\" target=\"_blank\" rel=\"noopener\" data-schema-attribute=\"\">The ES6 features website<\/a> has a nice list of many new features contrasted with the old way of doing things. If you want a place to just play around with the new features without the overhead of setting up a new project on your computer, you can easily start up a throw-away project on <a href=\"https:\/\/codepen.io\/\" target=\"_blank\" rel=\"noopener\">Codepen<\/a>.<\/p>\n<p>Finally, here are the links to the MDN pages of the features mentioned above. You can learn a lot more about them in these pages:<\/p>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/const\" target=\"_blank\" rel=\"noopener\">Keyword \u2018const\u2019<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/Arrow_functions\" target=\"_blank\" rel=\"noopener\">Arrow Functions<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Template_literals\" target=\"_blank\" rel=\"noopener\">Template Literals<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/for...of\" target=\"_blank\" rel=\"noopener\">Iterator \u2018for of\u2019<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/Default_parameters\" target=\"_blank\" rel=\"noopener\">Default Parameters<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/NumberFormat\" target=\"_blank\" rel=\"noopener\">Internationalization (i18n)<\/a><\/li>\n<\/ul>\n<h2 id=\"wrap-up\">6. Wrap-up<\/h2>\n<p>If you want to learn more about coding in general, there&#8217;s a whole host of articles available. Here is just a selection:<\/p>\n<ul>\n<li><a href=\"\/en\/blog\/web-development\/a-beginners-guide-to-the-10-most-popular-programming-languages\/\">A Beginner&#8217;s Guide to 10 of the Most Popular Programming Languages<\/a><\/li>\n<li><a href=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/web-development\/what-do-developers-use-github-for-heres-why-its-vital\/\">What Do Developers Use GitHub for? Here\u2019s Why It\u2019s Vital<\/a><\/li>\n<li><a href=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/web-development\/learning-to-program\/\"><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Learn to Code and Found Your Own Startup: A CEO's Advice&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:515,&quot;3&quot;:{&quot;1&quot;:0},&quot;4&quot;:[null,2,14281427],&quot;12&quot;:0}\">Learn to Code and Found Your Own Startup: A CEO&#8217;s Advice<\/span><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>What is ES6 and what&#8217;s it got to do with JavaScript? How can you implement it in your code? Discover all in this guide.<\/p>\n","protected":false},"author":55,"featured_media":216,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_lmt_disableupdate":"yes","_lmt_disable":"","footnotes":""},"categories":[5],"tags":[],"class_list":["post-4694","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development"],"acf":{"homepage_category_featured":false},"modified_by":"Matthew Deery","_links":{"self":[{"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/posts\/4694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/users\/55"}],"replies":[{"embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/comments?post=4694"}],"version-history":[{"count":2,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/posts\/4694\/revisions"}],"predecessor-version":[{"id":29001,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/posts\/4694\/revisions\/29001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/media\/216"}],"wp:attachment":[{"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/media?parent=4694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/categories?post=4694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/tags?post=4694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}