What is ES?
List of Versions from ES:
To date, ES has published nine versions, and the latest one (9th version) was published in the year 2018.
- ES1 1997
- ES2 1998
- ES3 1999
- ES4 Abandoned
- ES5 2009
- ES6 2015
- ES7 2016
- ES8 2017
- ES9 2018
ECMA Script's first three versions- ES1, ES2, and ES3 were yearly updates, whereas ES4 was never released due to political disagreements. After a decade, ES5 was eventually released with several additions
ES5 was released in 2009, ten years after the release of its previous version. Here is a list of features that have come with the es5 version.
1. ‘USE STRICT’ DIRECTIVE:
The earlier versions of JS allow the usage of undeclared variables. But when the es5 ‘use strict’ feature is used, an error is reported.
2. NEW METHODS IN AN ARRAY
- isArray(): This method checks if the object is an array or not and returns the result in true or false.
- forEach(): It executes the function for every element found in the array.
- map(): The map() method creates a new array by mapping every element of the array (on which the map is used).
- filter(): It creates a new array that contains elements which got filtered by making array elements pass some condition.
- reduce(): It applies a function to each element in an array and reduces the array to a single element.
NOTE: This example is the same as iterating over every element to calculate the sum.
- reduceRight(): It is quite same to reduce method, except for the traversal that happens from right to left.
- every(): This method checks a condition with every element of an array and if every element satisfies then returns true if at least one element doesn’t satisfy the condition then returns false.
- some(): It is same as every() method wherein some() checks for at least one element that gets satisfied with the condition and if found returns true and if none satisfies then false.
- indexOf(): Returns the index of the first match in an array. If not found then returns -1.
- lastIndexOf(): Same as indexOf but checks and returns from the other end of an array.
3. JSON SUPPORT
- parse(): It parses a JSON string that is like an object.
- stringify(): This method converts an object to a JSON string.
4. NEW METHODS IN A DATE
- now(): The now() method returns the number of milliseconds elapsed since 01-Jan-1970 UTC.
- valueOf(): It returns the primitive value of a date object.
5. GETTERS AND SETTERS:
The get method returns the value of a variable, and the set method sets the value of the variable.
6. PROPERTY METHODS
- Object.defineProperty(): This method lets the user define the property of an object and/or change its value.
Below is a list of the features that have come with ES6 version:
Till ES5, JS has only function scope and global scope with the introduction of let keyword in ES6, JS can now have block scope.
for...of is an alternative for both for...in and forEach() and loops iterable data structures like Arrays, Maps, Sets, and strings.
Provides default values to function parameters if no value or undefined is passed.
Rest Operator is used to handle function parameters. It uses three dots as its syntax (i.e …).
Spread Operator is used with arrays and its syntax is exactly the same as that of Rest Operator (ie …). It is used to split the contents of an array.
Destructuring helps in unpacking values from an array or an object.
It allows embedded expressions, which makes print statements easy.
Arrow Functions use => as its token and so are also called as Fat Arrow Functions. They are one line functions and are much like Lambda functions in programming languages like Java 8 and Python. Prior to =>, JS has a function keyword.
Promises are introduced in ES6 to handle Asynchronous Programming in a more elegant way. Before Promises, async calls were handled by Callbacks. Promises resolved the Call Back Hell.
Other features in ES6 include:
- Set, WeakSet, Map, WeakMap
- Binary and Octal
- Tail Call Optimization
ES7 or ECMAScript 2016 was released in the year 2016. This version gives suitable alternatives to already used functionalities.
EXPONENTIATION OPERATOR (**):
Returns true if an array includes a value, if not returns false.
This method pads a string with another string at the beginning.
This method pads a string with another string and makes the resulting string reach a given length. It adds spaces at the end of the string.
Await operator, applied only inside an async function, waits to be rejected or resolved by a promise.
It returns an array that contains the key-value pairs of a given object as an array.
A trailing comma is simply a comma that comes at the end of the last item in a list.
SHARED MEMORY AND ATOMICS:
The same data can be read and written on multiple threads using the SharedArrayBuffer constructor. Interruption during the process of reading or writing can be avoided by using Atomic objects. This allows the previous operation to finish prior to the next one.
An object is returned to the own property descriptors with get, set, writeable, configurable and enumerable attributes.
It returns an array of a given object’s own enumerable property values.
ES9 or ECMAScript 2018 is the latest update and was released in the year 2018.
An async iterable object can be used as a loop iteration with the help of for-await-of.
REGULAR EXPRESSION IMPROVEMENTS:
Ex: to parse a date in YYYY-MM-DD format.
The last arguments sent to a function are changed to an array using Rest parameters and the (...) notation can be used only for array operations.
In an opposite manner, an array is turned into separate arguments using the spread operator. These arguments can be transferred to a function.
The method then() is called after successfully resolving the promise, and the catch() method is declared if a problem occurs. Eventually, the code can be executed using the finally() method, irrespective of previous occurrences.