EcmaScript Examples

I’d like to share the ECMAScript Examples. ECMAScript is the standardized version of JavaScript. First edition of ECMAScript was introduced in 1997. Now, the ECMAScript version names are based on years, such as ECMAScript 2017 or ECMAScript 2018. Basically, the ECMAScript script determines the language principles. We can say it is the set of rules. JavaScript follows the ECMAScript rules and browser implements them. This means that if the browser or JavaScript engine is more compatible with ECMAScript, it will provide better results. Compatibility with the browsers is the biggest problem. Babel (a transpiler), Polyfills and other libraries are used to control the compatibility issue. All modern browsers support the ES6 now.

Regular Expression Examples:

Regular expressions define the patterns of string.

Here is a useful link.

Error Handling Examples:

Maps Examples:

It is a new data structure with unique key-value pairs (any data type) use to avoid the duplication of keys and maximize the performance in term of searching.

JSON Examples:

Getters and setters Examples:

Reflection and Object properties Examples:

Reflect API to inspect and manipulate the properties of objects. Like as in the following example we need to update ‘calcArea’ method in ‘obj’ object

Promises Examples:

JavaScript is synchronous programming language means to perform work one by one. Ajax and setTimeout are asynchronous. Promises convert the synchronous into asynchronous operation, means that asynchronous operations occur on the background while synchronous operations perform one-by-one. Promises are like an object which has three states:

    • Pending
    • Resolved
    • Rejected
const shortDelay = (s)=>
{
return new Promise((resolve,reject) => { if (s<6){ let ms =s*1000; setTimeout(()=>resolve(s),ms) } else { reject(s); } });
};
shortDelay(3).then( (res)=>console.log(“Resolved “+res), (rej)=>console.log(“Rejected “+rej)); // Resolved 3
shortDelay(10).then( (res)=>console.log(“Resolved “+res), (rej)=>console.log(“Rejected “+rej)); // Rejected 10

Modules Examples:

We can say modules are the container of a piece of code just like the JavaScript library and use this code by import/exports/require. This code contains classes, attributes, methods etc…

Arrow functions Examples:

Introduced with ES6 to create javascript function for shorter syntax, own scope and no binding of ‘this’. Also, fat arrow function has not constructor like function.

Spread operator (…) Examples:

The spread operator is introduced in ES6. By using this we can minimize our code (without the apply prototype).

De-structuring assignment Examples:

Differences between the ES5 and ES6:

All modern browser supports the ES5 but on the other hand, we need to transpile the ES6. There are syntax differences. ES6 having the more features.

Pipe Function Examples:

The purpose of pipe() is call the number of the functions in sequence. pipe() is the alternative of function nesting.

Block-scoped variable declarations (let) Examples: