HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
ECMAScript 2015 was the second major revision to JavaScript
Safari 10 and Edge 14 were the first browsers to fully support ES6:
Chrome 58 | Edge 14 | Firefox 54 | Safari 10 | Opera 55 |
Jan 2017 | Aug 2016 | Mar 2017 | Jul 2016 | Aug 2018 |
The let
keyword allows you to declare a variable with
block scope.
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.
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.
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 allows a short syntax for writing function expressions.
You don't need the function
keyword, the return
keyword, and the
curly brackets.
// ES5
var x = function(x, y) {
return x * y;
}
// ES6
const x = (x, y) => x * y;
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.
Using const
is safer than using var
, because a function expression is
always a constant value.
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:
const x = (x, y) => { return x * y };
Learn more about Arrow Functions in the chapter: JavaScript Arrow Function.
The JavaScript for/of
statement loops
through the values of an 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:
const
, let
, or var
.
Looping over an Array
const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
text += x + " ";
}
Looping over a String
let language = "JavaScript";
let text = "";
for (let x of language) {
text += x + " ";
}
Learn more in the chapter: JavaScript Loop For/In/Of.
Being able to use an Object as a key is an important Map feature.
// Create Objects
const apples = {name: 'Apples'};
const bananas = {name: 'Bananas'};
const oranges = {name: 'Oranges'};
// Create a new Map
const fruits = new Map();
// Add new Elements to the Map
fruits.set(apples, 500);
fruits.set(bananas, 300);
fruits.set(oranges, 200);
Learn more about Map objects in the the chapter: JavaScript Maps.
// 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 are templates for JavaScript Objects.
Use the keyword class
to create a class.
Always add a method named constructor()
:
class ClassName {
constructor() { ... }
}
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.
When you have a class, you can use the class to create objects:
const myCar1 = new Car("Ford", 2014);
const myCar2 = new Car("Audi", 2019);
Learn more about classes in the the chapter: JavaScript Classes.
A Promise is a JavaScript object that links "Producing Code" and "Consuming Code".
"Producing Code" can take some time and "Consuming Code" must wait for the result.
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 */ }
);
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.
A JavaScript Symbol is a primitive datatype 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:
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
ES6 allows function parameters to have default values.
function myFunction(x, y = 10) {
// y is 10 if not passed or undefined
return x + y;
}
myFunction(5); // will return 15
The rest parameter (...) allows a function to treat an indefinite number of arguments as an array:
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);
The includes()
method returns true
if a string contains a specified value,
otherwise false
:
let text = "Hello world, welcome to the universe.";
text.includes("world") // Returns true
The startsWith()
method returns true
if a string begins with a specified value, otherwise false
:
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello") // Returns true
The endsWith()
method returns true
if a string ends with a specified value, otherwise false
:
var text = "John Doe";
text.endsWith("Doe") // Returns true
The Array.from()
method returns an Array object from any object with a length
property or any iterable object.
Create an Array from a String:
Array.from("ABCDEFG") // Returns [A,B,C,D,E,F,G]
The keys()
method returns an Array Iterator object with the keys of an array.
Create an Array Iterator object, containing the keys of the array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
const keys = fruits.keys();
let text = "";
for (let x of keys) {
text += x + "<br>";
}
The find()
method returns the value of the first array element that passes a
test function.
This example finds (returns the value of ) the first element that is larger than 18:
const numbers = [4, 9, 16, 25, 29];
let first =
numbers.find(myFunction);
function myFunction(value, index, array) {
return
value > 18;
}
Note that the function takes 3 arguments:
The findIndex()
method returns the index of the first array element that
passes a test function.
This example finds the index of the first element that is larger than 18:
const numbers = [4, 9, 16, 25, 29];
let first =
numbers.findIndex(myFunction);
function myFunction(value, index, array) {
return
value > 18;
}
Note that the function takes 3 arguments:
ES6 added the following methods to the Math object:
Math.trunc()
Math.sign()
Math.cbrt()
Math.log2()
Math.log10()
Math.trunc(x)
returns the integer part of x:
Math.trunc(4.9); // returns 4
Math.trunc(4.7); // returns 4
Math.trunc(4.4); // returns 4
Math.trunc(4.2); // returns 4
Math.trunc(-4.2); // returns -4
Math.sign(x)
returns if x is negative, null or positive:
Math.sign(-4); // returns -1
Math.sign(0); // returns 0
Math.sign(4); // returns 1
Math.cbrt(x)
returns the cube root of x:
Math.cbrt(8); // returns 2
Math.cbrt(64); // returns 4
Math.cbrt(125); // returns 5
Math.log2(x)
returns the base 2 logarithm of x:
Math.log2(2); // returns 1
Math.log10(x)
returns the base 10 logarithm of x:
Math.log10(10); // returns 1
ES6 added the following properties to the Number object:
EPSILON
MIN_SAFE_INTEGER
MAX_SAFE_INTEGER
let x = Number.EPSILON;
let x = Number.MIN_SAFE_INTEGER;
let x = Number.MAX_SAFE_INTEGER;
ES6 added 2 new methods to the Number object:
Number.isInteger()
Number.isSafeInteger()
The Number.isInteger()
method returns true
if the argument is an integer.
Number.isInteger(10); // returns true
Number.isInteger(10.5); // returns false
A safe integer is an integer that can be exactly represented as a double precision number.
The Number.isSafeInteger()
method returns true
if the argument is a safe integer.
Number.isSafeInteger(10); // returns true
Number.isSafeInteger(12345678901234567890); // returns false
Safe integers are all integers from -(253 - 1) to +(253 - 1).
This is safe: 9007199254740991. This is not safe: 9007199254740992.
ES6 added 2 new global number methods:
isFinite()
isNaN()
The global isFinite()
method returns false
if the argument is Infinity
or NaN
.
Otherwise it returns true
:
isFinite(10/0); // returns false
isFinite(10/1); // returns true
The global isNaN()
method returns true
if the argument is NaN
. Otherwise it returns false
:
isNaN("Hello"); // returns true
Create an Array Iterator, and then iterate over the key/value pairs:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
const f = fruits.entries();
for (let x of f) {
document.getElementById("demo").innerHTML += x;
}
The entries()
method returns an Array Iterator object with key/value pairs:
[0, "Banana"]
[1, "Orange"]
[2, "Apple"]
[3, "Mango"]
The entries()
method does not change the original array.
Modules are imported in two differen ways:
Import named exports from the file person.js:
import { name, age } from "./person.js";
Import a default export from the file message.js:
import message from "./message.js";
Learn more about Modules in: JavaScript Modules.