ECMAScript 6

What is ECMAScript 6?

ECMAScript 6 is also known as ES6 and ECMAScript 2015.

Some people call it JavaScript 6.

This chapter will introduce some of the new features in ES6.

  • JavaScript let
  • JavaScript const
  • JavaScript Arrow Functions
  • JavaScript Classes
  • Default parameter values
  • Array.find()
  • Array.findIndex()
  • Exponentiation (**) (EcmaScript 2016)

Browser Support for ES6 (ECMAScript 2015)

Safari 10 and Edge 14 were the first browsers to fully support ES6:

ChromeEdgeFirefoxSafariOpera
Chrome 58Edge 14Firefox 54Safari 10Opera 55
Jan 2017Aug 2016Mar 2017Jul 2016Aug 2018

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

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;
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrow Functions</h2>

<p>With arrow functions, you don't have to type the function keyword, the return keyword, and the curly brackets.</p>

<p>Arrow functions are not supported in IE11 or earlier.</p>

<p id="demo"></p>

<script>
const x = (x, y) => x * y;
document.getElementById("demo").innerHTML = x(5, 5);
</script>

</body>
</html>

Classes

ES6 introduced classes.

A class is a type of function, but instead of using the keyword function to initiate it, we use the keyword class, and the properties are assigned inside a constructor() method.

Use the keyword class to create a class, and always add a constructor method.

The constructor method is called each time the class object is initialized.

class Car {
  constructor(brand) {
    this.carname = brand;
  }
}
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Class</h2>

<p>In this example we demonstrate a simple class definition and how to use it.</p>

<p id="demo"></p>

<script>
class Car {
  constructor(brand) {
    this.carname = brand;
  }
}

mycar = new Car("Ford");

document.getElementById("demo").innerHTML = mycar.carname;
</script>

</body>
</html>

Default Parameter Values

ES6 allows function parameters to have default values.

<!DOCTYPE html>
<html>
<body>

<h2>Default Parameter Values</h2>

<p id="demo"></p>

<script>
function myFunction(x, y = 10) {
  // y is 10 if not passed or undefined
  return x + y;
}
document.getElementById("demo").innerHTML = myFunction(5);
</script>

</body>
</html>

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:

Note that the function takes 3 arguments:

  • The item value
  • The item index
  • The array itself
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array.find()</h2>

<p id="demo"></p>

<script>
var numbers = [4, 9, 16, 25, 29];
var first = numbers.find(myFunction);

document.getElementById("demo").innerHTML = "First number over 18 is " + first;

function myFunction(value, index, array) {
  return value > 18;
}
</script>

</body>
</html>

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:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array.findIndex()</h2>

<p id="demo"></p>

<script>
var numbers = [4, 9, 16, 25, 29];
var first = numbers.findIndex(myFunction);

document.getElementById("demo").innerHTML = "First number over 18 has index " + first;

function myFunction(value, index, array) {
  return value > 18;
}
</script>

</body>
</html>

New Number Properties

ES6 added the following properties to the Number object:

  • EPSILON 2.220446049250313e-16
  • MIN_SAFE_INTEGER -9007199254740991
  • MAX_SAFE_INTEGER 9007199254740991

New Number Methods

ES6 added 2 new methods to the Number object:

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

New Global Methods

ES6 also added 2 new global number methods:

  • isFinite()
  • isNaN()

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

Exponentiation Operator

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

x ** y produces the same result as Math.pow(x,y):

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

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

Source : https://www.w3schools.com/js/js_es6.asp

One thought on “ECMAScript 6

Leave a Reply

Your email address will not be published. Required fields are marked *