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
PacktPub.com
Contributors
Preface
Index

Arrays


There are some new properties added to the global Array object and to its instances to make working with arrays easier. Arrays in JavaScript lacked features and capabilities when compared with programming languages such as Python and Ruby. Let's take a look at some popular methods associated with arrays and their use cases.

The Array.from(iterable, mapFunc, this) method

The Array.from() method creates a new array instance from an iterable object. The first argument is a reference to the iterable object. The second argument is optional and is a callback (known as the Map function) that is called for every element of the iterable object. The third argument is also optional and is the value of this inside the Map function.

Here is an example to demonstrate this:

let str = "0123";
let arr = Array.from(str, value => parseInt(value) * 5);
console.log(arr);

The output is:

[0, 5, 10, 15].

Array.from would be extremely useful in converting an "array-like" structure to the actual array. For example...