Javascript ES6


ECMAScript 2015 is the second significant update to JavaScript.

ECMAScript 2015 is commonly referred to as ES6 and ECMAScript 6.

This section talks about the key aspects of ES6.

New Features in ES6


JavaScript let

The let keyword lets you create a variable that only exists within a specific block of code.

Learn more about the let keyword in the chapter on JavaScript Let.


JavaScript const

The const word lets you set up a constant in JavaScript, which means a variable that won't change its value.

Constants are like 'let' variables, but their values cannot be altered.

Learn more about the const keyword in the section on JavaScript Const.


Arrow Functions

Arrow functions make it easy to write concise function expressions using a shorter syntax.

You can exclude the function and return keywords, as well as the curly brackets.

Arrow functions don't have their own this. They aren't ideal for defining object methods.

Arrow functions don't get moved up in code. You need to create them before using them.

Using const is better than var because a function expression remains constant.

If your function consists of only one statement, you can skip using the return keyword and curly brackets. It's a good practice to include them, though.

Explore further details on Arrow Functions in this section: JavaScript Arrow Function.


The Spread (...) Operator

The ... operator helps to spread out a list or array into individual elements.

The "..." operator lets you take items from a list and use them as separate inputs for functions.


The For/Of Loop

The JavaScript code using for/of helps to go through the values of objects that can be iterated.

The for/of allows you to go through collections that can be looped over, like Arrays, Strings, Maps, NodeLists, and others.

The syntax for the for/of loop is as follows:

for (variable of iterable) {
  // code block to be executed
}

Variable - Each time it loops, the next property's value is given to the variable. You can define a variable using const, let, or var.

Iterable - An object with properties that can be iterated over.

Looping over an Array

Looping over a String

Find additional information in this section: JavaScript Loop For/In/Of.


JavaScript Maps

Using an Object as a key is a crucial feature in Maps.

Discover more about Map objects and understand the distinction between a Map and an Array in the chapter: JavaScript Maps.


JavaScript Sets

Find out more about Set objects in the chapter: JavaScript Sets.


JavaScript Classes

JavaScript Classes act as blueprints for creating JavaScript Objects.

Utilize the term class to form a class.

Always include a function called constructor():

Syntax

class ClassName {
  constructor() { ... }
}

Example

class Car {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }
}

The code snippet provided defines a class called "Car."

The class initially has two properties: "name" and "year."

A JavaScript class is not the same as an object.

It's a template for JavaScript objects.


Using a Class

When you possess a class, you can utilize it to generate objects:

Explore further details about classes in the chapter on JavaScript Classes by visiting this link.


JavaScript Promises

A Promise in JavaScript connects "Producing Code" with "Consuming Code."

"Writing code" may require time, and other parts of code have to wait for its outcome.

Promise Syntax

const myPromise = new Promise(function(myResolve, myReject) {
// "Producing Code" (May take some time)

  myResolve(); // when successful
  myReject();  // when error
});

// "Consuming Code" (Must wait for a fulfilled Promise).
myPromise.then(
  function(value) { /* code if successful */ },
  function(error) { /* code if some error */ }
);

Discover additional information about Promises in the chapter: JavaScript Promises.


The Symbol Type

A JavaScript Symbol is a basic type, similar to Number, String, or Boolean.

It signifies a special "secret" code that cannot be unintentionally reached by any other code.

For example, if various programmers want to include a person.id attribute to a person object from someone else's code, they might blend their values together.

Creating unique identifiers is made simple by using Symbol().

Note

Symbols are always one of a kind.

If you make two symbols with the identical description, they will possess distinct values:

Symbol("id") == Symbol("id"); // false

Default Parameter Values

ES6 enables setting default values for function parameters.


Function Rest Parameter

The rest parameter (…) enables a function to handle a variable number of arguments as an array.


String.includes()

The includes() function checks if a given string has a particular value. It gives back true if the string contains the value and false if it doesn't.


String.startsWith()

The startsWith() function checks if a given string starts with a particular value.It gives back true if the string starts with the given value, and false if it doesn't.


String.endsWith()

The endsWith() function checks if the end of a given string matches a particular value. It returns true if there is a match, and false otherwise.


Array.from()

The function Array.from() makes an array from an object that has a length property or any object you can loop through.


Array keys()

The keys() method gives you an Array Iterator containing the keys of an array.


Array find()

The find() method gives back the value of the initial array element that satisfies a specified test function.

This example locates and gives the value of the initial element greater than 18.


Array findIndex()

The findIndex() function gives the position of the initial array item that meets a specific condition.

This example locates the position of the initial element surpassing the value 18.


New Math Methods

ES6 introduced new functions to the Math object:

  • Math.trunc()
  • Math.sign()
  • Math.cbrt()
  • Math.log2()
  • Math.log10()

The Math.trunc() Method

Math.trunc(x) gives you the whole number part of the value x:


The Math.sign() Method

The Math.sign(x) function tells us whether x is negative, zero, or positive.


The Math.cbrt() Method

The code Math.cbrt(x) calculates the cube root of the value x.


The Math.log2() Method

Math.log2(x) calculates the logarithm of x to the base 2:


The Math.log10() Method

The code Math.log10(x) calculates the logarithm of x to the base 10.


New Number Properties

ES6 introduced new features to the Number object.

  • EPSILON
  • MIN_SAFE_INTEGER
  • MAX_SAFE_INTEGER

New Number Methods

ES6 introduced two additional methods to the Number object:

  • Number.isInteger()
  • Number.isSafeInteger()

The Number.isInteger() Method

The Number.isInteger() function gives a true result when the input is a whole number.


The Number.isSafeInteger() Method

A safe integer is a whole number that can be accurately represented as a double precision number.

The Number.isSafeInteger() function shows true when the input is a safe whole number.

Safe integers are numbers between -(2^53 - 1) and +(2^53 - 1). For example, 9007199254740991 is a safe integer, but 9007199254740992 is not.


New Global Methods

ES6 introduced two additional global number methods.

  • isFinite()
  • isNaN()

The isFinite() Method

The worldwide isFinite() function gives back false if the input is Infinity or NaN.

Otherwise, it gives back true:


The isNaN() Method

The worldwide isNaN() function tells us if a value is NaN or not. It gives back true if the input is NaN, and false otherwise.


Object entries()

The entries() function gives you an Array Iterator object containing key/value pairs.

[0, "Banana"]
[1, "Orange"]
[2, "Apple"]
[3, "Mango"]

The entries() method doesn't modify the original array.


Modules

There are two ways to bring in modules:

Find out more about modules here: JavaScript Modules.