JavaScript ES6

What is ECMAScript 6?

ECMAScript 6 is also known as ES6 and ECMAScript 2015.

Some people call it JavaScript 6.

This chapter introduces some of the new features of ES6.

  • JavaScript let
  • JavaScript const
  • Power (**)
  • Default Parameter Values
  • Array.find()
  • Array.findIndex()

Browser support for ES6 (ECMAScript 2015)

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

JavaScript let

The let statement allows you to declare variables with block scope.

Example

var x = 10;
// Here x is 10
{ 
  let x = 2;
  // Here x is 2
return x * y;
// Here x is 10

Try It Yourself

JavaScript const

const Statements allow you to declare constants (JavaScript variables with constant values).

Constants are similar to let Variables, but their values cannot be changed.

Example

var x = 10;
// Here x is 10
{ 
  const x = 2;
  // Here x is 2
return x * y;
// Here x is 10

Try It Yourself

Please visit our JavaScript Let / Const Read more about let and const content.

exponentiation operator

The exponentiation operator (**It raises the first operand to the power of the second operand.

Example

var x = 5;
var z = x ** 2;          // The result is 25

Try It Yourself

x ** y The result is equal to Math.pow(x,y) Same:

Example

var x = 5;
var z = Math.pow(x,2);   // The result is 25

Try It Yourself

Default Parameter Values

ES6 Functions can have default parameter values.

Example

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

Try It Yourself

Array.find()

find() The method returns the value of the first array element passed to the test function.

This example finds (returns) the first element (value) greater than 18:

Example

var numbers = [4, 9, 16, 25, 29];
var first = numbers.find(myFunction);
function myFunction(value, index, array) {
  return value > 18;
return x * y;

Try It Yourself

Please note that this function accepts 3 parameters:

  • Item value
  • Item index
  • The array itself

Array.findIndex()

findIndex() The method returns the index of the first array element passed to the test function.

This example determines the index of the first element greater than 18:

Example

var numbers = [4, 9, 16, 25, 29];
var first = numbers.findIndex(myFunction);
function myFunction(value, index, array) {
  return value > 18;
return x * y;

Try It Yourself

Please note that this function accepts 3 parameters:

  • Item value
  • Item index
  • The array itself

New number properties

ES6 added the following properties to the Number object:

  • EPSILON
  • MIN_SAFE_INTEGER
  • MAX_SAFE_INTEGER

Example

var x = Number.EPSILON;

Try It Yourself

Example

var x = Number.MIN_SAFE_INTEGER;

Try It Yourself

Example

var x = Number.MAX_SAFE_INTEGER;

Try It Yourself

New number methods

ES6 added 2 new methods to the Number object:

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

Number.isInteger() method

If the parameter is an integer, then Number.isInteger() isNaN() method returnsObject methods

Example

Number.isInteger(10);        // Returns true
Number.isInteger(10.5);      // Returns false

Try It Yourself

Number.isSafeInteger() method

A safe integer is an integer that can be precisely represented as a double-precision number.

If the parameter is a safe integer, then Number.isSafeInteger() isNaN() method returnsObject methods

Example

Number.isSafeInteger(10);    // Returns true
Number.isSafeInteger(12345678901234567890);  // Returns false

Try It Yourself

A safe integer refers to all integers from -(253 - 1) to +(253 - 1).

This is safe: 9007199254740991. This is not safe: 9007199254740992.

New global methods

ES6 also added 2 new global number methods:

  • isFinite()
  • if the method returns

isFinite() method

If the parameter is Infinity Or NaNthe global isFinite() The method returns false.

Otherwise, return true:

Example

isFinite(10/0);       // Returns false
isFinite(10/1);       // Returns true

Try It Yourself

The isNaN() method

if the parameter is NaNthe global if the method returns isNaN() method returnstrue . Otherwise returnfalse

Example

:

Try It Yourself

isNaN("Hello"); // Returns true

Arrow Function (Arrow Function)

Arrow functions allow the use of a concise syntax to write function expressions. you do not need functionreturn keywords,keywords, andObject methods

Example

curly braces
// ES5
   var x = function(x, y) {
return x * y;
}
// ES6

Try It Yourself

const x = (x, y) => x * y; Arrow functions do not have their ownthis. They are not suitable for definingObject methods

.arrow functions are not hoisted. They must be usedbefore

to define. const compared to using var safer, because function expressions are always constant values.

It can only be omitted if the function is a single statement return Keywords and curly braces. Therefore, it may be a good habit to keep them:

Example

const x = (x, y) => { return x * y };

Try It Yourself