Book Image

Learn ECMAScript - Second Edition

By : MEHUL MOHAN, Narayan Prusty
Book Image

Learn ECMAScript - Second Edition

By: MEHUL MOHAN, Narayan Prusty

Overview of this book

Learn ECMAScript explores implementation of the latest ECMAScript features to add to your developer toolbox, helping you to progress to an advanced level. Learn to add 1 to a variable andsafely access shared memory data within multiple threads to avoid race conditions. You’ll start the book by building on your existing knowledge of JavaScript, covering performing arithmetic operations, using arrow functions and dealing with closures. Next, you will grasp the most commonly used ECMAScript skills such as reflection, proxies, and classes. Furthermore, you’ll learn modularizing the JS code base, implementing JS on the web and how the modern HTML5 + JS APIs provide power to developers on the web. Finally, you will learn the deeper parts of the language, which include making JavaScript multithreaded with dedicated and shared web workers, memory management, shared memory, and atomics. It doesn’t end here; this book is 100% compatible with ES.Next. By the end of this book, you'll have fully mastered all the features of ECMAScript!
Table of Contents (18 chapters)
Title Page


JavaScript is an integral part of web development and server-side programming. Understanding the fundamentals of JavaScript can not only help a person create interactive web applications but also help set up web servers, create mobile applications through frameworks such as React Native, and even create desktop applications using frameworks such as electronJS.

This book introduces the fresh and core concepts of JavaScript in the form of ECMAScript 2017 (ES8), which includes everything you'll need to get started with JavaScript and have a basic-to-advanced understanding so that you can implement everything mentioned in the first paragraph.

Who this book is for

This book is for anybody who is absolutely new to JavaScript and is willing to learn this technology. This book can also be used by people who are familiar with old JavaScript and want to level up their knowledge to the latest standard and use techniques. For more advanced users, this book can be used to brush up concepts such as modularity, web workers, and shared memory.

What this book covers

Chapter 1, Getting Started with ECMAScript, discusses what ECMAScript really is and why we call it ECMAScript and not JavaScript. It also discusses how to create variables, perform basic operations, and provides new ways to do those operations in ES8.

Chapter 2, Knowing Your Librarydemonstrates all the functions you need to know as a beginner and intermediate JavaScript developer to work smoothly on various kinds of projects. The functions taught in this chapter are universal and generic functions, which you'll be able to understand and apply anywhere they're necessary for your code.

Chapter 3, Using Iterators, covers how to iterate over iteratable things in JavaScript the proper way. We discuss Symbol, a new native JavaScript type, what it is, and why we need it. We also discuss the tail call optimization technique, which is implemented by browsers to speed up the code.

Chapter 4, Asynchronous Programmingexplores modern ways to implement asynchronous programming and compares it with the not-so-beautiful past approaches, which included callback hell. It'll teach you about implementing asynchronous programming in a synchronous way.

Chapter 5, Modular Programming, discusses modularizing your JavaScript code into different files so that it is easy to reuse and debug individual modules. We start with primitive and third-party solutions available earlier and then cover the native support browsers are bringing to the world.

Chapter 6Implementing the Reflect API, demonstrates information about the Reflect API provided in JavaScript, which basically helps to manipulate the properties and methods of the objects.

Chapter 7Proxies, introduces a new implementation in JavaScript, that is, proxies over objects. It has a number of advantages, such as hiding private properties, setting default values for object properties and methods, and making awesome custom features. Such as Python-like array slicing for JavaScript.

Chapter 8Classes, explores classes, how they're implemented, inheritance in classes, and how finally classes is just a syntatic sugar over the function implementation only. This is important because classes make the code more readable and understandable to people coming from an OOP background.

Chapter 9JavaScript on the Web, explores the basics of using JavaScript on websites, some popular APIs exposed by browser on the web to the developers, and how JavaScript can be used to interact with the DOM to manipulate things on a web page.

Chapter 10, Storage APIs in JavaScript, explores the available storage APIs in web browsers and shows how to make use of them to store data locally on the user's computer.

Chapter 11Web and Service Workers, discusses web workers available in HTML5, service workers for progressive web apps, and shows how to use these workers efficiently to distribute loads of tasks.

Chapter 12Shared Memory and Atomics, teaches us how to harness a multithreaded environment provided by web workers using shared memory to allow blazingly fast access to memory by the web workers through SharedArrayBuffer. It covers some of the common problems related to threads sharing the same data, and also provides solutions to those problems. 

To get the most out of this book

Although it's not strictly required, it'll be great if you know a little about HTML/CSS and a little about how to create basic web pages using it.

You should be familiar with a modern browser (Chrome or Firefox is preferred) on a desktop/laptop.

To get the most out of this book, don't just read the book alone; keep other study sources open and implement as many of the demo and example codes as you can.

Download the example code files

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Look carefully, we are not executing counter() again and again."

A block of code is set as follows:

let myCounter = counter(); // returns a function (with count = 1)
myCounter(); // now returns 2
myCounter(); // now returns 3

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

var ob1 = {
   prop1 : 1,
    prop2 : {
        prop2_1 : 2 
Object.freeze( ob1 );

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "However, you can specify the distance to move, that is history.go(5); is equivalent to the user hitting the forward button in the browser five times."


Warnings or important notes appear like this.


Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit


Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit