Three Out Of This World Node 6 Features You Need to Know About

April 27, 2016
Written by

node

Node.js version 6.0.0 shipped yesterday and brings with it a ton of stellar ECMAScript 2015 features. Here are three that will make your life easier and are worth getting excited about.

Function Defaults

How many times have you written boilerplate code that looks something like this:

function beginTeleportation (who, options) {
  options = options || {}
}

Well the days of checking for the existence of function arguments are over. Default function parameters let you define a default value for function arguments right in the function declaration.

function beginTeleportation (who, options = {}) {

}

In this example if options isn’t passed in it will be assigned to a new object.

This is works great for other parameters and values as well.

function sayCosmicGreeting (greeting = 'Greetings', name = 'Earthling') {
  console.log(`${greeting}, ${name}`)
}

sayCosmicGreeting() // Greetings, Earthling
sayCosmicGreeting('Salutations', 'Martian') // Salutations, Martian

Rest Parameters

Passing a variable number of arguments into a function has in the past required you to use the arguments keyword.

function sayCosmicGreetingTo (greeting) {
  const beings = arguments.slice(1)
  beings.forEach(being => {
    console.log(`${greeting}, ${being}`)
  })
}

sayCosmicGreetingTo('Hello', 'Earthling', 'Martian', 'Neptunian')

The problem, as shown by this example, is it is easy to run into a TypeError: arguments.slice is not a function error because arguments isn’t an actual Array, but array-like. To make this work you have to know to use the call function on the Array’s prototype:

Array.prototype.slice.call(arguments, 1)

But this can be pretty obscure for beginners. It’s also very unclear that any other arguments are being passed into the function.

Rest parameters fix this by giving you an actual instance of Array.

function sayCosmicGreetingTo (greeting, ...beings) {
  beings.forEach(being => {
    console.log(`${greeting}, ${being}`)
  })
}

sayCosmicGreetingTo('Hello', 'Earthling', 'Martian', 'Neptunian')

Destructuring

A common pattern in JavaScript is for a function to take in an object for easy configuration options. Before, those options had to manually be pulled out of the object and assigned to variables.

function beginDepartureSequence (options) {
  const captain = options.captain
  const ship = options.ship
  const destination = options.destination
  console.log(`Blast off sequence initiated for Captain ${captain} on ship ${ship} flying to ${destination}`)
}

beginDepartureSequence({
  captain: 'Rey',
  ship: 'Millennium Falcon',
  destination: 'Jakku'
})

Now with Node 6 we can destructure that object right into variables where order doesn’t matter.

function beginDepartureSequence (options) {
  const { destination, captain, ship } = options
  console.log(`Blast off sequence initiated for Captain ${captain} on ship ${ship} flying to ${destination}`)
}

beginDepartureSequence({
  captain: 'Rey',
  ship: 'Millennium Falcon',
  destination: 'Jakku'
})

Even better we can do it right in the function definition. This allows you to be as declarative as named parameters but with an object.

function beginDepartureSequence ({ destination, ship, captain }) {
  console.log(`Blast off sequence initiated for Captain ${captain} on ship ${ship} flying to ${destination}`)
}

beginDepartureSequence({
  captain: 'Rey',
  ship: 'Millennium Falcon',
  destination: 'Jakku'
})

Wrapping it Up

While none of these are completely game changing they make our lives better as developers by allowing us to write clearer and less code.

These aren’t the only features that shipped with this release. Check out these great resources to learn more.

Try them out and let me know which your favorites are.

If you like JavaScript, join us May 24th and 25th for some awesome talks at SIGNAL by Kassandra Perch, Katy Moe, and many more. You’ll also get the opportunity to launch yourself into space at our insane carnival for coders, $bash. Use promo code ezaneski20 for 20% off your ticket.