ES6 Introduction - Part 1

A brief overview to understand new features in ES6, including Strings, Objects, let

Posted by Aditya Pratap Singh on June 9, 2015

Better String Support:

The new addition to deck of ES 6 string methods are:

  • includes()- returns true if given text is found anywhere inside the string else false
  • startsWith() – returns true if given text is found at the beginning of the string else false
  • endsWith()- returns true if given text is found at the end of the string else false
  • Repeat() - Accepts a single integer argument for the no of times to repeat the string

Better Object utilities:

Object.is(): Better comparison using Object.is() and avoids the inaccuracy in identically equals operator (===) for below scenarios:

  1. (===) considers +0 and -0 as equal while Object.is() does not
  2. (===) returns (NaN === NaN) as false while Object.is treats all NaN as same and returns true

Block Bindings using let:

In most languages variables are created when declared while in JavaScript variables are hoisted at the top of function regardless of declaration. variable hoisting in JavaScript causes in bugs if we don’t understand the underlying logic of hoisting.

To avoid this scenario and introduce the block level scope, ES 6 has introduced the let keyword.

The let keyword scopes the variable to current code block.

var:

let:

One of the most important application of let can be observed in for loops where the loop control variable persists, even after the loop ends.

ES5: