Skip to main content

ES6 features

Questions for this topic can be :

  • Tell me some ES6 features.
  • Tell me some JavaScript bad parts that ES6 fixed

block scope

ES5 only have function level scope. To create a block level scope we have to create an IIFE. But ES6 introduced 2 new keywords 'let' and 'const'. These two provides block level scoping (curly braces to scope). So now variables created with in if block has no scope out side of it. As a result of this the use of IIFE is eliminated.

Duplicate variable declaration

var keyword is not restricting creation of dupliate variables. So a developer may accidentally declare two variables with the same name. But let and const restricts creation of duplicate variables.

Arrow Function

ES6 introduced arrow functions. Arrow functions don't have their own this value. Arrow functions don't have their own argument array..

Dealing with arguments

In ES5 arguments acts like an Array. We can loop over it but is not an actual array. So Array functions like sort, slice are not available.

function mySort(){
var args = Array.prototype.slice.call(arguments);
return args.sort(function(a,b){ return a-b})
}

console.log(mySort(1,5,2,8,11))

In ES6, a new feature called Rest Parameters introduced. It can be represented as ...args. Rest parameters is an array. So we can use all the array functions to it.

function mySort(...args){
return args.sort((a,b) => a-b)
}

console.log(mySort(1,5,2,8,11))

strict mode

Strict Mode is introduced in ES5 but it is optional. In ES6 many features need Strict Mode. So most people and tools like babel automatically add "use strict" at the top of file putting the whole JS Code in strict mode and force us to write better JavaScript code.