This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here!
Smartphone icons created by Freepik - Flaticon
9.3 JS 2015 (ES6)
ECMAScript 6, also known as ES6 and ECMAScript 2015, was the second major revision to JavaScript.
This chapter describes the most important features of ES6.
New Features in ES6
JavaScript let
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.
return to top of page
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.
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.
return to top of page
Arrow Functions
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.
return to top of page
The For/Of Loop
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:
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
var cars = ["BMW", "Volvo", "Mini"];
var x;
for (x of cars) {
document.write(x + "<br>");
}
Looping over a String
var txt = "JavaScript";
var x;
for (x of txt) {
document.write(x + "<br>");
}
Learn more in the chapter: JavaScript Loop For/In/Of.
JavaScript Classes
JavaScript Classes are templates for JavaScript Objects.
Use the keyword class to create a class.
Always add a method named constructor() :
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:
let myCar1 = new Car("Ford", 2014);
let myCar2 = new Car("Audi", 2019);
Learn more about classes in the the chapter: JavaScript Classes.
return to top of page
JavaScript Promise Object
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.
Promise Syntax
let 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 */ }
);
let 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.
return to top of page
The Symbol Type
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 instace, 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;
Symbols are always unique.
If you create two symbols with the same description they will have different values.
Symbol("id") == Symbol("id") // false
return to top of page
Default Parameter Values
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
return to top of page
Function Rest Parameter
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);
return to top of page
Array.find()
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:
var numbers = [4, 9, 16, 25, 29];
var first = numbers.find(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Note that the function takes 3 arguments:
The item value
The item index
The array itself
return to top of page
Array.findIndex()
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:
var numbers = [4, 9, 16, 25, 29];
var first = numbers.findIndex(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Note that the function takes 3 arguments:
The item value
The item index
The array itself
return to top of page
New Number Properties
ES6 added the following properties to the Number object:
EPSILON
MIN_SAFE_INTEGER
MAX_SAFE_INTEGER
var x = Number.EPSILON;
var x = Number.MIN_SAFE_INTEGER;
var x = Number.MAX_SAFE_INTEGER;
return to top of page
New Number Methods
ES6 added 2 new methods to the Number object:
Number.isInteger()
Number.isSafeInteger()
The Number.isInteger() Method
The Number.isInteger() method returns true if the argument is an integer.
Number.isInteger(10); // returns true
Number.isInteger(10.5); // returns false
The Number.isSafeInteger() Method
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.
return to top of page
New Global Methods
ES6 added 2 new global number methods:
The isFinite() Method
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 isNaN() Method
The global isNaN() method returns true if the argument is NaN . Otherwise it returns false:
isNaN("Hello"); // returns true
return to top of page
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Strings - String.trim()
JS Arrays - isArray()
JS Array.forEach()
JS Array.map()
JS Array.filter()
JS Array.reduce()
JS Array.reduceRight()
JS Array.every()
JS Array.some()
JS Array.indexOf()
JS Array.lastIndexOf()
JS JSON.stringify()
JS Date.now()
JS Getters and Setters part 1
JS Getters and Setters part 2
JS Getters and Setters part 3
JS New Object Property Methods part 1
JS New Object Property Methods part 2
JS New Object Property Methods part 3
JS Property Access on Strings part 1
JS Property Access on Strings part 2
JS Strings Over Multiple Lines part 1
JS Strings Over Multiple Lines part 2
JS let
JS const
JS Arrow Functions part 1
JS Arrow Functions part 2
JS For Of Loop - Looping over an Array
JS For Of Loop - Looping over a String
JS Classes - Using a Class
JS Promise - Using a Promise
JS Using JavaScript Symbol()
JS Functions Default Parameter Values
JS Functions - The Rest Parameter
JS Arrays - The find() Method
JS Arrays - The findIndex() Method
JS Number Object Properties - EPSILON
JS Number Object Properties - MIN_SAFE_INTEGER
JS Number Object Properties - MAX_SAFE_INTEGER
JS Numbers - The isInteger() Method
JS Numbers - The isSafeInteger() Method
JS Numbers - The isFinite() Method
JS Numbers - The isNaN() Method
JS Exponentiation Operator
JS Exponentiation Operator - Math.pow()
JS Exponentiation Assignment (**=)
JS Arrays - The includes() Method
JS Strings - The padStart() Method
JS Strings - The padEnd() Method
JS Object Methods - Object.entries() method
JS Object Methods - Object.values() method
JS Async Functions
JS Strings - The trimStart() Method
JS Strings - The trimEnd() Method
JS Objects - The fromEntries() Method
JS Arrays - The flat() Method
JS Arrays - The flatMap() Method
JS Stable Sort
JS JSON - Revised stringify()
JS Strings - Separator Symbols
JS Functions - The toString() Method
JS Numbers - Create a BigInt
JS Numbers - BigInt typeof
JS Strings - The matchAll() Method part 1
JS Strings - The matchAll() Method part 2
JS Strings - The matchAll() Method part 3
JS Operators - The ?? Operator
JS Operators - The ?. Operator
JS Strings - The replaceAll() Method part 1
JS Strings - The replaceAll() Method part 2
JS Numbers - Numeric Separator (_) part 1
JS Numbers - Numeric Separator (_) part 2
JS Numbers - Numeric Separator (_) part 3
JS Arrays - Array at()
JS Strings - The at() Method