Three Out Of This World Node 6 Features You Need to Know About
Time to read: 2 minutes
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:
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.
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.
Rest Parameters
Passing a variable number of arguments into a function has in the past required you to use the arguments
keyword.
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.
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.
Now with Node 6 we can destructure that object right into variables where order doesn’t matter.
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.
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.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.