Shorten functions with ES6 features

Shorten functions with ES6 features

ES6 implements features which use a more expressive closure syntax and more intuitive expression interpolation that can shorten your functions.

Consider this function.

function sayHello(name) {
  return 'Hello, ' + name + '.';

Using template literals string interpolation, we can re-write the function like so.

functon sayHello(name) {
  return `Hello, ${name}.`;

Notice the backticks instead of quotes around the string.

We can further shorten it using arrow functions.

var sayHello = name => `Hello, ${name}.`;

Using this syntax, the function is shortened drastically, without losing any code clarity.

The function above looks nice, but there’s one last change I would personally make to make the code even easier to reason about.

const sayHello = name => `Hello, ${name}.`;

I swapped var for const. Constants cannot be re-assigned, so this function will always do the same thing no matter when it’s called during run-time.