bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/Debugging, Projects, and Reference
JavaScript•Debugging, Projects, and Reference

Javascript 2015 (ES6)

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Javascript 2015 (ES6)?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// ___ x is 10 {
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Array Destructuring
Object Destructuring
New Features in JavaScript 2015 (ES6)

ECMAScript 2015

The second major revision to JavaScript .

ECMAScript 2015 is also known as ES6.

New Features in JavaScript 2015 (ES6)

FeatureDescription
The let keywordDeclares a variable with block scope
The const keywordDeclare a contant immutable variable
Arrow FunctionsAllows short syntax for writing function expressions
{a,b} = OperatorAssigns object properties to variables (object destructuring)
[a,b] = OperatorAssigns array values to variables (array destructuring)
... OperatorSpreads an array or iterable into individual elements
For/ofLoops through the values of iterable objects
Map ObjectsObject with key-value pairs, similar but different from objects
Set ObjectsArray that stores unique values
ClassesTemplates for JavaScript Objects
PromisesObject representing the completion of an asynchronous operation
SymbolA unique "hidden" identifier that no code can access
Default ParametersAllows default values for function parameters
Rest ParametersAllows functions to treat an indefinite number of arguments
String.includes()Returns true if a string contains a specified value
String.startsWith()Returns true if a string begins with a specified value
String.endsWith()Returns true if a string ends with a specified value
Array entries()Returns an iterator key/value pairs from an array
Array.from()Creates an array from a string
Array keys()Returns an iterator with the keys of an array
Array find()Returns the value of the first element that passes a test
Array findIndex()Returns the index of the first element that passes a test
Object.assign()Copies properties from a source object to a target object
RegExp /uEnables full Unicode support in a regular expression
RegExp /yPerforms a "sticky" search from the lastIndex property
isFinite()returns true if the argument is not Infinity or NaN
IsNaN()Returns true if the argument is NaN
ModulesAllows for breaking up your code into separate files
ReflectObject for low-lewel operation on objects
ProxyObject that wraps other objects for control operations

Math Features

FeatureDescription
Math.trunc(x)Returns the integer part of x
Math.sign(x)Returns -1, 0 or 1 (x is negative, null or positive)
Math.cbrt(x)Returns the cube root of x
Math.log2(x)Returns the base 2 logarithm of x
Math.log10(x)Returns the base 10 logarithm of x

Number Features

FeatureDescription
Number.EPSILONThe difference between 1 and the smallest number greater than 1
Number.MIN_SAFE_INTEGERMinimum value that can be precisely represented
Number.MAX_SAFE_INTEGERMaximum value that can be precisely represented
Number.isInteger()Returns true if the argument is an integer
Number.isSafeInteger()Returns true if the argument is a safe integer

Browser Support

JavaScript 2015 is supported in all modern browsers since June 2017 :

Chrome 51Edge 15Firefox 54Safari 10Opera 38
May 2016Apr 2017Jun 2017Sep 2016Jun 2016

ES6 is not supported in Internet Explorer.

JavaScript let

The let keyword allows you to declare a variable with block scope.

Example

var x = 10;
// Here x is 10 {
let x = 2;
// Here x is 2
}
// Here x is 10

Read more about let in the chapter: JavaScript Let .

JavaScript const

The const keyword allows you to declare a constant (a JavaScript variable with a constant value).

Constants are similar to let variables, except that the value cannot be changed.

Example

var x = 10;
// Here x is 10 {
const x = 2;
// Here x is 2
}
// Here x is 10

Read more about const in the chapter: JavaScript Const .

Arrow Functions

Arrow functions is a short syntax for writing function expressions .

You don't need the function keyword, the return keyword, or the curly brackets .

Before Arrow:

let myFunction = function(a, b) {return a * b}

With Arrow

let myFunction = (a, b) => a * b;

Arrow functions do not have their own this . They are not well suited for defining object methods .

Arrow functions are not hoisted. They must be defined before they are used.

You can only omit the return keyword and the curly brackets if the function is a single statement. Because of this, it might be a good habit to always keep them:

Example

// This will not work
let myFunction = (x, y) =>  { x * y } ;
// This will not work
let myFunction = (x, y) =>  return x * y ;
// Only this will work
let myFunction = (x, y) => { return x * y };

Learn more about Arrow Functions in the chapter: JavaScript Arrow Function .

Object Destructuring

Destructuring assignment makes it easy to assign array values and object properties to variables.

Example

// Create an Object
const person = {
  firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue"
};
// Destructuring Assignment
let { firstName, age } = person;

When destructuring an object, you must use the same name for the variables as the corresponding object keys (names).

The order of the keys (names) does not matter.

Array Destructuring

Destructuring assignment makes it easy to assign array values and object properties to variables.

Example

// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// Destructuring Assignment
let [fruit1, fruit2] = fruits;

The Spread (...) Operator

...
...

Example

const q1 = ["Jan", "Feb", "Mar"];
const q2 = ["Apr", "May", "Jun"];
const q3 = ["Jul", "Aug", "Sep"];
const q4 = ["Oct", "Nov", "Dec"];
const year = [...q1, ...q2, ...q3, ...q4];

The For/Of Loop

The JavaScript for/of statement loops through the values of iterable objects.

for/of lets you loop over data structures that are iterable such as Arrays, Strings, Maps, NodeLists, and more.

The for/of loop has the following syntax:

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

variable - For every iteration the value of the next property is assigned to the variable. Variable can be declared with const , let , or var .

iterable - An object that has iterable properties.

Looping over an Array

Example

const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
  text += x + " ";
}

Looping over a String

Example

let language = "JavaScript";
let text = "";
for (let x of language) {
  text += x + " ";
}

Learn more in the chapter: JavaScript Loop For/In/Of .

JavaScript Maps

A Map is an object that stores key-value pairs, similar to objects, but with differences:

  • Keys can be of any data type (objects, functions, primitive values), unlike plain objects where keys are strings.
  • Maintains the original insertion order of keys.

Being able to use an Object as a key is an important Map feature.

Example

const fruits = new Map([ ["apples", 500], ["bananas", 300], ["oranges", 200]
]);

Learn more about Map objects, and the difference between a Map and an Array, in the the chapter: JavaScript Maps .

JavaScript Sets

A Set is an object that stores unique values of any type (primitive values, functions, objects).

A Set can only contain unique values. An attempt to add a duplicate value will be ignored.

Example

// Create a Set
const letters = new Set();
// Add some values to the Set letters.add("a"); letters.add("b"); letters.add("c");

Learn more about Set objects in the the chapter: JavaScript Sets .

JavaScript Classes

JavaScript Classes are templates for JavaScript Objects.

Use the keyword class to create a class.

Syntax

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

Example

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

The example above creates a class named "Car".

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

A JavaScript class is not an object.

It is a template for JavaScript objects.

Using a Class

When you have a class, you can use the class to create objects:

Example

const myCar1 = new Car("Ford", 2014);
const myCar2 = new Car("Audi", 2019);

Learn more about classes in the the chapter: JavaScript Classes .

JavaScript Promises

A JavaScript Promise is an object representing the completion or failure of an asynchronous operation and its values.

It is a placeholder for a value that may not yet be available, providing a structured way to handle asynchronous code.

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 */ }
);

Example Using a Promise

const myPromise = new Promise(function(myResolve, myReject) {
  setTimeout(function() { myResolve("I love You !!"); }, 3000);
});
myPromise.then(function(value) {
  document.getElementById("demo").innerHTML = value;
});

Learn more about Promises in the the chapter: JavaScript Promises .

The Symbol Type

A JavaScript Symbol is a primitive data type just like Number, String, or Boolean.

It represents a unique "hidden" identifier that no other code can accidentally access.

For instance, if different coders want to add a person.id property to a person object belonging to a third-party code, they could mix each others values.

Using Symbol() to create a unique identifiers, solves this problem:

Example

const person = {
  firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue"
};
let id = Symbol('id');
person[id] = 140353;
// Now person[id] = 140353 // but person.id is still undefined

Symbols are always unique.

If you create two symbols with the same description they will have different values:

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

Default Parameter Values

ES6 allows function parameters to have default values.

Example

function myFunction(x, y = 10) {
  // y is 10 if not passed or undefined
  return x + y;
}
myFunction(5); // will return 15

Function Rest Parameter

The rest parameter (...) allows a function to treat an indefinite number of arguments as an array:

Example

function sum(...args) {
  let sum = 0;
  for (let arg of args) sum += arg;
  return sum;
}
let x = sum(4, 9, 16, 25, 29, 100, 66, 77);

String.includes()

The includes() method returns true if a string contains a specified value, otherwise false :

Example

let text = "Hello world, welcome to the universe.";
text.includes("world")    // Returns true

Previous

ECMAScript 2016

Next

JavaScript 2009 (ES5)