Implementing splat args in JavaScript

Typically in JavaScript you’ll define the arguments you expect in a function like so:

function sayHello(message, recipient) {
    // Do something exciting here...
  }
  

But what happens when you want to pass an arbitrary amount of arguments to a function, or what is often termed “splat args” in other languages?

Well, JavaScript provides a special property called arguments that is scoped to the function call. You can implement a form of splat args with JavaScript using the following approach:

function sayHello(message, recipient) {
    // args is an array of arguments passed into the function.
    var args = Array.prototype.slice.call(arguments, sayHello.length);
    // Do something exciting here...
  }
  
  // Now you can call sayHello with any number of arguments:
  sayHello('Hello there!', 'John Smith', some, extra, args);
  

This short line takes the arguments list and calls Array.slice to get all the extra arguments beyond the position arguments.

sayHello.length is the length of the arguments passed into the function so we’re able to get all arguments after the last positional argument and put them into an array.

Until the spread operator and rest parameters is implemented in ECMAScript 6, we’ll have to stick with the above, slightly hacky approach.

However, when these two changes are universally implemented, we’ll be able to do the following instead:

function sayHello(message, recipient, ...args) {
    // args will now be available and will
    // contain an array of the arguments passed in.
  }
  

Or, as my friend Alex points out, you can always use CoffeeScript.

← Read all articles