Passing Arrays as Function Arguments

Samantha Ming
Feb 18 ยท 4 min read

If you want to pass an array into a variadic function. You can use ES6 spread to turn that array into a list of arguments. Yay, so much cleaner and no useless null from the old apply way ๐Ÿ‘

function sandwich(a, b, c) { 
console.log(a) // '๐Ÿž'
console.log(b) // '๐Ÿฅฌ'
console.log(c) // '๐Ÿฅ“'
}
const food = ['๐Ÿž', '๐Ÿฅฌ', '๐Ÿฅ“'];// Old way
sandwich.apply(null, food);
// โœ… ES6 way
sandwich(...food);

Using it with Math functions

The ability to turn an array into a list of arguments is super handy with the Math functions.

Example: Find the Largest Number

Letโ€™s say you want to find the largest number using the Math.max() function.

const largest = Math.max(5, 7, 3, 4);console.log(largest); // 7

But rarely, would you pass in individual values. More likely, you would want to find the maximum element in an array. So the question now is, how do you pass an array of values into a function that accepts individual arguments and NOT an array?

This would be terrible:

const numbers = [5, 7, 3];// ๐Ÿคฎ Yuck!
Math.max(numbers[0], numbers[1], numbers[2]);
// โŒ And this won't work
Math.max(numbers); // NaN

Lucky for us, we can use ES6โ€™s Spread operator!

const numbers = [5, 7, 3];// ๐Ÿ˜ Much Better!
Math.max(...numbers); // 7

What spread is doing here is taking the array element and expanding or unpacking it into a list of arguments for our variadic function.

const numbers = [5, 7, 3];console.log(...numbers); // 5 7 3

Explaining spread in non-dev terms

If you find this spread-ing thing still confusing. Maybe let me try to explain it with Russian nesting dolls. So I like to think of the array as Russian nesting dolls. And what spread does is:

  1. It unpacks (spread) the nested dolls into individual dolls.
  2. And now you have all these individual dolls (arguments) to place nicely in your display case (function).

Not sure if this explanation helps? Leave a comment if it does, and Iโ€™ll start explaining programming concepts in fun ways like this ๐Ÿ˜†

Passing Multiple Arrays as Function Arguments

Another superpower spread has is combining arrays.

const one = [1,2,3];
const two = [4,5,6];
const merged = [...one, ...two];
// [ 1, 2, 3, 4, 5, 6 ]

So we can use this superpower to pass multiple arrays as function arguments ๐Ÿ’ช

const one = [1,2,3];
const two = [4,5,6];
Math.max(...one, ...two); // 6

For those keeners, wondering if you can pass in 3 arrays. Well, you betcha! Itโ€™s like the energizer bunny, it keeps going and going and going โ€ฆ. (This post is not sponsored by Energizer lol. But that can change, hit me up Energizer. Me want some sponsor money ๐Ÿ˜‚)

const one = [1,2,3];
const two = [4,5,6];
const three = [2,100,2];
Math.max(...one, ...two, ...three); // 100

What is a variadic function?

So you may notice I use the term variadic functions. The computer science folks will have probably heard this term. But for the rest of the cool bees like myself ๐Ÿ˜, it may not be so familiar. A variadic function is a function that accepts an infinite or variable number of arguments. And the Math.max() function is one of those variadic function.

Community Feedback

Brad Forbes: You can use spread right alongside non-spread values too

const three = [2,100,2];Math.max(1, 2, ...three);

Resources

Share

Thanks for reading โค

Say Hello!

DailyJS

JavaScript news and opinion.

Samantha Ming

Written by

Front End Developer ๐Ÿ‘ฉโ€๐Ÿ’ป I love learning new things and sharing them with my community. Follow me to catch my weekly code tidbits ๐Ÿ”ฅ

DailyJS

DailyJS

JavaScript news and opinion.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and weโ€™ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium โ€” and support writers while youโ€™re at it. Just $5/month. Upgrade