ES6
ES6 refers to version 6, of the ECMAScript which is a JavaScript standard meant to ensure the interoperability of Web pages across different Web browsers. It was published in June 2015, therefore you might find it renamed as ES2015.
It is a major enhancement to the JavaScript language and adds many more features intended to make large-scale software development easier.
Along with these, it brings a so-called syntactical sugar, which is syntax with the same behavior but designed to make things easier to read or to express.
var vs. let vs. const
The main difference between var
and let
is that instead of being function scoped, let
is block scoped. What that means is that a variable created with the let
keyword is available inside the “block” that it was created in as well as any nested blocks. “Block”, means anything surrounded by a curly brace {}
like in a for
loop or an if
statement.
let
declarations are not hoisted.
const
prevents redeclaring the same variable
=== vs ==
the 3 symbol comparison operator ===
or !==
adds a type check between the two operands
You can find more about how comparison operators evaluate in various scenarios at the following link:
https://dorey.github.io/JavaScript-Equality-Table/
Arrow functions
Arrows functions are both syntactical sugar and have a structural impact on how the code is being executed.
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.
The { }
and ( )
and return
are optional, but may be required.
For example, if you have multiple arguments or no arguments, you'll need to re-introduce parentheses around the arguments.
An arrow function does not have its own bindings to this
or super
, and should not be used as methods.
Template literals
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
They were called "template strings" in prior editions of the ES2015 specification.
They allow also simple evaluations using the ${ eval } syntax.
this keyword
In JavaScript this
is a special keyword and it basically refers to the object it belongs to. It was used before ES6 too but the introduction of arrow functions changes its behavior. this
is an implicit parameter, that is when every execution context is created, this
is automatically added to the local scope as a parameter with the value of what is on the left-hand side of .
- calling function. If the "dot" is missing when calling the function, it will default to the global object.
However, it has different values depending on it is used:
In a method,
this
refers to the owner object. If the method's syntax is written with an ES6 arrow function, then it refers to the enclosing scope of the owner object. In other words, in arrow functions,this
is lexically scoped, meaning it refers to the object where the function was declared, not the called.In the global scope or in a function,
this
refers to the global object. However, usingstrict mode
,this
isundefined
.In an event, such as a DOM
onclick
, it refers to the element that received the event.Core function methods such as
call()
,apply()
orbind()
can referthis
to any object passed as an argument.
.call()
The call()
method calls (executes) a function with a given this
value and arguments provided. In practice, it is used to write methods that can be reused on different objects. Both this
and additional arguments are optional.
.apply()
This methods is similar to call()
, the only difference is that the arguments are passed as an array. The method is quite useful even if this
is not passed, in scenarios where we need to pass an unknown or large number of arguments.
.bind()
This method is also similar, meaning it takes this
and additional arguments but returns a new function, which is not executed at that point.
Spread operator and Rest parameters
The spread operator ...
allows an iterable variable or expression to be expanded for scenarios where more arguments are expected.
ES6 Utility functions
ES6 introduced, among many other features, a set of utility functions that we'll find very useful for writing functional programming code. Here are some of them:
.forEach()
This is an array method, explicitly Array.prototype.forEach()
, that executes a provided (callback) function once for each array element. The callback takes the following arguments in order: current value, index of the current value, and the whole array. The last two are optional.
forEach()
returns undefined
.map()
This is a method similar to .forEach() but instead, it returns a new array, of the same length as the one called upon, where each element is the result of the callback function.
Another shorthand example to create a new array containing the square of each element of the original array:
.filter()
Array.prototype.filter()
also takes a callback function as an argument but this tests each element of the array. It returns a new array, possibly of smaller length, containing only the elements that passed the test.
.reduce()
This method is a bit more complex, but also very powerful. It accumulates the result of a callback function over each element of the array. In other words, it reduces that array to a single value.
The callback takes the following arguments: the accumulator, the current value being iterated, and optionally the index of the current value and the array itself.
The method takes, along the callback, an additional argument which will be considered the initial value of the accumulator.
More examples of ES6 utility functions
Array.from() converts an iterable object to an array.
Array.find() returns the first element in an array that satisfies a condition.
Array.findIndex() returns the index of the first element in an array that satisfies a condition.
Array.includes() checks if an array contains a specific element.
Array.some() checks if any element in an array satisfies a condition.
Object.assign() copies the properties of one object to another object.
Object.keys() returns an array containing the names of all of the properties of an object.
Object.values() returns an array containing the values of all of the properties of an object.
Object.entries() returns an array containing two-element arrays, where each element is a property name and value from the original object.
Math.max() returns the largest number of a given set of numbers.
Math.min() returns the smallest number of a given set of numbers.
Math.floor() rounds a number down to the nearest integer.
Math.ceil() rounds a number up to the nearest integer.
Math.round() rounds a number to the nearest integer.
Last updated
Was this helpful?