THE WORLD'S LARGEST WEB DEVELOPER SITE
×

JS Tutorial

JS HOME JS Introduction JS Where To JS Output JS Statements JS Syntax JS Comments JS Variables JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Scope JS Events JS Strings JS String Methods JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Dates JS Date Formats JS Date Get Methods JS Date Set Methods JS Math JS Random JS Booleans JS Comparisons JS Conditions JS Switch JS Loop For JS Loop While JS Break JS Type Conversion JS Bitwise JS RegExp JS Errors JS Debugging JS Hoisting JS Strict Mode JS this Keyword JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words JS Versions JS Version 6 JS JSON

JS Forms

JS Forms Forms API

JS Objects

Object Definitions Object Properties Object Methods Object Constructors Object Prototypes

JS Functions

Function Definitions Function Parameters Function Invocation Function Call Function Apply Function Closures

JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM CSS DOM Animations DOM Events DOM Event Listener DOM Navigation DOM Nodes DOM Collections DOM Node Lists

JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS AJAX

AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

JS JSON

JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Objects JSON Arrays JSON Parse JSON Stringify JSON PHP JSON HTML JSON JSONP

JS Examples

JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Quiz JS Certificate

JS References

JavaScript Objects HTML DOM Objects


JavaScript 6 - ECMAScript 6


What is ECMAScript 6

ECMAScript 6 is also known as ES6 and ECMAScript 2015

Some people like to call it JavaScript 6.

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

  • JavaScript let
  • JavaScript const
  • JavaScript default parameter values

JavaScript let

The let statement allows you to declare a variable inside brackets {} scope.

Redeclaring a variable using var can impose problems:

Example

var x = 10;
// Now x is 10
{
    var x = 2;
    // Now x is 2
}
// Now x is 2
Try it Yourself »

Redeclaring a variable using let can solve the problems:

Example

var x = 10;
// Now x is 10
{
    let x = 2;
    // Now x is 2
}
// Now x is 10
Try it Yourself »


JavaScript const

The const statement is used to declare a variable with a constant value.

It is similar to let, except that the value of the variable cannot be changed.

The variable must be initialized when it is declared.

Example

var x = 10;
// Now x is 10
{
    const x = 2;
    // Now x is 2
}
// Now x is 10
Try it Yourself »

Default Parameter Values

ES6 allows function parameters to have default values.

Example

function myFunction(x, y = 10) {
    // y is 10 if not passed or undefined
    return x + y;
}
myFunction(5); // will return 15
Try it Yourself »

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()

The Number.isInteger() Method

The Number.isInteger() method returns true if the argument is an integer.

Example

Number.isInteger(10);        // returns true
Number.isInteger(10.5);      // returns false
Try it Yourself »

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.

Example

Number.isSafeInteger(10);    // returns true
Number.isSafeInteger(12345678901234567890);  // returns false
Try it Yourself »

Safe integers are 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()
  • isNan

The isFinite() Method

The global isFinite() method returns false if the argument is Infinity or NaN.

Otherwise it returns true:

Example

isFinite(10/0);       // returns false
isFinite(10/1);       // returns true
Try it Yourself »

The isNaN() Method

The global isNan() method returns true if the argument is NaN. Otherwise it returns false:

Example

isNaN("Hello");       // returns true
Try it Yourself »

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.

Example

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

// ES6
const x = (x, y) => x * y;
Try it Yourself »

Arrow functions do not have their own this. They are not well suited for defining object methods.

Arrow functions must be defined before they are used. Using const is safer than using var, because a function expression is a constant value.

You can only ommit the return keyword and the curly brackets if the function is a single statement.

It might be a good habbit to keep them:

Example

const x = (x, y) => { return x * y };
Try it Yourself »