This Is Why Learning New JavaScript ES6 Syntax Is So Famous!
Photo: Stackify

This Is Why Learning New JavaScript ES6 Syntax Is So Famous!

What is ES6?

ECMASCRIPT 2015 or ES6 has introduced many changes to JavaScript. JavaScript ES6 has introduced new syntax and new awesome features to make your code more modern and more readable.

It allows you to write less code and do more.

ES6 introduced many great features like arrow functions, template strings, class destructions, modules… and many more.

Let’s dive into more reasons of its popularities.

Reason 1: You can write it now for all Browsers.

ES6 is now supported in all major browsers. Here is the list of Browsers support for ES6.

Browser Support For ES6
Photo Courtesy: W3Schools.com

Reason 2: Full Backward Browser Compatible

It has support for backward compatible version of JavaScript in current and older browsers or environments. JavaScript being a rich ecosystem, has hundreds of packages on the package manager (NPM), and it has been adopted worldwide. To make sure ES5 JavaScript packages should always work in the presence of ES6, they decided to make it 100% backward compatible.

This has one major benefit. You can start writing ES6 along with your existing JavaScript ES5. This also help you to start slowly embracing new features and adopting the aspects of ES6, which can make your life easier as a programmer.

To support full backward browser compatibility, here is a very cool project called Babel.

What is Babel?

Babel is a JavaScript Transpiler that converts edge JavaScript into plain old ES5 JavaScript that can run in any browser. For more details about Babel, please click the below links.

Learn more about Babel.

Babel Usage Guide. How to setup Babel?

Reason 3: ES6 is faster in some cases.

I am going to share one performance benchmark for ES6. ES6 performed more better or even hit the performance benchmark for the same function written in ES5. You can visit this link as well to check the ES6 performance benchmark.

Performance of ES6 features relative to the ES5 baseline operations per second

Reason 4: Doing more with writing less code

As said above, now you can do lot more with writing less code in your ES6. You can write more cleaner and concise code in ES6. Here is few examples of code syntax of ES6.

1. Arrow Functions in ES6

No need to write function and return keywords anymore. This is one of the more awesome feature of ES6, makes your code looks more readable, more structured, and looks like modern code. You don’t need to write old syntax anymore.

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target.

Arrow Function Body

One of the benefit of arrow function is to have either concise body or the usual block body.

In a concise body, only an expression is mentioned in a single line with the implicit return value. In a block body, you must use explicit return keyword, normally with curly braces { return .. }.

Line Break in Arrow function

ES6 will throw an error if there is an improper line break in arrow function. Ideally, it should be single line statement. If you want to use it as multi line statement, use the proper curly braces or brackets.

Arrow function example in ES6 vs ES5

2. Extended Parameter Handling in ES6

2.1 Default Parameter Value

Simple and smart way of passing default values of function parameters. Now, functions can be defined with default parameters values. If you miss to pass the function parameter, then missing or undefined values will be initialized with default parameters.

Prevent you from undefined error

Default parameter will prevent you from getting the undefined error. If you forget to write the parameter, it won’t return the undefined error, because the parameter is already defined in the function parameter.

Default parameter example in ES6 vs ES5

2.2 Rest Parameter

Easy way to pass rest parameters with three dots (…) with parameter name. With the help of rest parameter, you can pass an indefinite number of arguments as an array.

A function last parameter prefixed with ... which will cause all remaining arguments to be placed within a standard JavaScript array.

Only a last parameter can be a rest parameter.

Rest parameter are like an Array

One of the major difference between argument object and rest parameter is that, rest parameter are like real array instance, where argument object are not like a real array. This means that, you can apply array methods like sort, map, forEach or pop on it directly.

Rest parameter example in ES6 vs ES5

3. Template Literals in ES6

String Interpolation

Concatenating the string in a more cleaner way. Template literals are string literals, which allow concatenated expression. String interpolation is one of the interesting feature of ES6. Prior to ES6, we use double or single quotes to concatenate string expression, which sometimes looks very weird and buggy.

Now, in ES6, template literals are enclosed by back-ticks () character instead of single or double quotes. You can find back-ticks at the top left of your keyboard under the esc key.

Template literals has placeholders with $ sign and curly braces like ${expression}. $ sign is mandatory for interpolation.

Example of string concatenation in ES6 vs ES5

4. Enhanced Object Properties in ES6

Property Shorthand

Shorter syntax for defining object properties. Prior to ES6, every object property needs to be either getter-setter or key-value pair. This has been completely changed in ES6. In ES6, there is a concise way for defining the object properties. You can define complex object properties in a much cleaner way now.

Method Properties

Support for method notation in object properties definitions. In the same way, which we discussed above, we can now define object methods concisely and in a much cleaner way.

Reason 5: New Built-In Methods In ES6

Object Property Assignment

There is a new function to assign enumerable properties of one or more source objects into a destination object.

Array Element Finding

There is a new function to find an element in an array.

String Repeating

There is a new string repeating functionality as well.

String Searching

New string functions to search for a sub-string

Number Type Checking

There are new functions for checking non-numbers and finite numbers.

Number Safety Checking

There is an in-built function to check whether an integer number is in the safe range.

Number Truncation

There is a mathematical function to truncate a floating number to its integral parts, completely dropping the fractional part.

Summary

Javascript surely isn’t a perfect language. It has various imperfections. In the course of recent years, developers have gotten increasingly more involvement with the JavaScript ES5, which has lead to enhancements. ES6 brings many engrossing features that were not seen in previous version like ES5.

Further Reading

Adesh

Hi, I am Adesh. I am a senior software engineer having more than 12 years of software development experience. I am a full stack developer and interested in writing the technical post on programming. I am currently working in New York City area.

This Post Has One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu