Five-minute crash course in ES6, ES7 and Node

I wrote this short article for frontend developers who know JavaScript and use it for the frontend, but have little idea about the new features of the language introduced in ES6 and ES7, and have never touched Node.js.

I’ll make it quick.

Arrow functions

Instead of the keyword function before arguments, use an arrow => after the arguments.

The shorter syntax is useful when the function is passed as an argument:

The last example works because:

  • When the function has exactly one argument, brackets around (item) are optional; and
  • If the braces {} are omitted and the function has only one statement, the keyword return is optional.

Variables

In addition to var, you can also use let and const.

  • let has a smaller scope than var — it is limited to the block surrounding it.
  • const is the same as let, but the value cannot be reassigned.

Template strings

In addition to single quotes and double quotes, you can now use backticks. This is useful when combining strings with variable values and function outputs:

async/await

Finally, no more callback functions. Write asynchronous code as if it was synchronous:

Good to know:

  • await can only be placed inside a function marked async.
  • async-await uses Promises under the hood. That means you can await any function that returns a Promise.
  • You can usetry-catch as usual to catch errors in async functions.

Destructuring

Destructuring lets you assign multiple variables at once.

That also works with functions that return arrays:

for…of; entries()

No more clumsy looping over arrays. Forget for loops, forgetfor...in + if. Forget Array.forEach that can’t return a value, break or continue.

If you need the i variable, use .entries():

Shorthand objects

If you create an object from variables, and the key names are exactly the same as the variable names, then you don’t have to repeat the same word twice:

Function arguments

Functions can now take default values for arguments:

Similar to the params keyword in C#, functions can take a variable number of arguments:

Expand arrays

Three dots before an array expands it into separate values:

You can use this to quickly concatenate several arrays:

Classes

JavaScript still uses prototype-based classes, but there’s a cleaner syntax to create them:

Other useful tricks

Run async tasks in parallel

If you need to do multiple async operations, this will be terribly slow, because you await each operation before starting a new one:

Instead, we can run all three async operations in parallel, and hopefully get the results three times faster:

Remove duplicates from array

A new built-in JavaScript object called Set creates a collection of unique values. You can then convert it back into an Array:

And finally, Node.js

With Node, you can run ordinary JavaScript code in the console. Just open cmd and type node my-code.js.

You can split code into multiple files, and include them as necessary using module.exports and require.

Export the code that you want to reuse:

And now import this code in another JavaScript file:

Conclusion

That should be enough to get started. But there’s more! Explore and have fun.