Learn a few syntaxes included in ES6 in 5 minutes

Tram Ho

What is ES6?

ES6 is an abbreviation of ECMAScript 6, this is considered a set of advanced techniques of Javascript and is the latest version of the ECMAScript standard. According to wikipedia, ECMAScript is a brand specification scripting language standardized by Ecma International through ECMA-262 and ISO/IEC 16262 . It was created to standardize JavaScript, to promote many independent implementations. JavaScript remains the most famous ECMAScript implementation since this standard was published, with other well known implementations including JScript and ActionScript. ECMAScript is commonly used for client-side scripting on the World Wide Web, and it is increasingly used to write server applications and services using Node.js.
In this article, I would like to introduce you to some of the functions in ES6 that we should apply to daily programming because it can save you time from shorter syntaxes. and many other benefits …

1, The keyword “let”

In javascript, the most common way to declare a variable is to use the var keyword, but in ES6, a variable declared let is supported. Why was the let keyword born? This is simply because if we want to use a variable in a word block, use this keyword.
For example :
Usually if you use the var keyword

but with the keyword let

Furthermore let does not allow reassigning the value of a variable. For example :
with var

also let

2, Keyword const

This const keyword to declare a read-only variable. For example :

Once the const variable has been declared, it cannot be reassigned. The assignment must take place at the same time as the variable declaration

3, Define the default parameter

We can declare default parameters when knowing a function

We can also overwrite (rewrite) the value of the parameter. For example

We can also use expressions within default parameters. For example :

4, Spread and Rest Operators

Spread Operator allows converting a string into multiple arguments (in case of calling with the function) or into multiple elements (for arrays). The syntax it uses is ... For example:

Or rather, we can pass the value of an array instead of parameters in a function. For example :

Rest operators use it as its name, temporarily translated in Vietnamese as các tham số còn lại . The meaning is as follows.

5, Declare multiple values ​​at the same time

6, Arrow Functions

This is a fairly new syntax for using a function declaration

Even in cases where the function only uses a single statement, we can shorten it as follows

7, Template literals

The way of concatenating strings in ES6 is quite simple. With syntax es5.

in es6 will be more optimal

How to write a paragraph of text on multiple lines

8, Short method definition (Method definition shorthand)

The function keyword may be ignored when assigning methods on an object.

9, Loop

References

Share the news now

Source : Viblo