ES6 Documentation
What is ECMAScript 6
ECMAScript (or ES) is a trademarked scripting-language specification standardized by Ecma International in ECMA-262 and ISO/IEC 16262. It was created to standardize JavaScript, so as to foster multiple independent implementations. JavaScript has remained the best-known implementation of ECMAScript since the standard was first published, with other well-known implementations including JScript and ActionScript.[3] ECMAScript is commonly used for client-side scripting on the World Wide Web, and it is increasingly being used for writing server applications and services using Node.js.
ECMAScript 6 is also known as ES6 and ECMAScript 2015.
Some people like to call it JavaScript 6.
This chapter will introduce some of the new features in ES6.
- JavaScript let
- JavaScript const
- Exponentiation (**)
- Default parameter values
- Array.find()
- Array.findIndex()
JavaScript let
The let statement 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
JavaScript const
The const statement 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 let and const in JS Let / Const.
Exponentiation Operator
The exponentiation operator (**) raises the first operand to the power of the second operand.
var x = 5;
var z = x ** 2; // result is 25
x ** y produces the same result as Math.pow(x,y):
var x = 5;
var z = Math.pow(x,2); // result is 25
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
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
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
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;
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
New Global Methods
ES6 also 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
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 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 };