JS argument specification

I understand that JS has a liberal ‘arguments optional’ type approach when it comes to function calls.

That is assume one had a function defined as:

function example(a, b, c) {

Of course, in the end it all depends on what the function ultimately ‘does’-- However example calls of the like:

example(1, 1);
example(1, 1, “something”);

At least would not throw any errors. This ‘overloading’ is a great feature of the language to have. However, given that, out of curiosity, is there anyway I can thus make a function call where I can somehow ‘only’ feed/affect, say prop ‘c’ ?


example(, ,“something”);

And preclude any changes to the first two, or must I provide some value and then put in handlers for the ‘no change’ case. I would understand how to do this with handlers, and I imagine this is the only way but I didn’t know if there was any ‘trick’ to do this… It would actually make a lot of sense, I think.

What is your question?

Sorry Randell,

Hit ‘enter’ by accident. See updated post above.

Yes, it’s called partial application, and it’s a bit of a pain in JavaScript, because it a. depends on the order of the arguments and b. JS functions can have an arbitrary number of arguments. With your example, you want to prefill the last of three arguments: you’d take your example function, and return a new function with that filled. It’s not idiomatic in JavaScript - you ideally want your example to be curried, then you apply a function that flips the arguments, then you return a new function with (what is now) the first argument (sorry if that sounds like gibberish). In a language like F#, it’s really easy, the language is built to do what you want to do, in JS it’s…not as easy.

Functions that take single arguments are easy (obviously), functions that take two arguments are pretty easy if you want to fill the first argument or it doesn’t matter which order the arguments come in, like:

> const add = (a, b) => a + b
> const add10 = add.bind(null, 10)
> add10(5)

More than two, it’s a pain in the ass.

I was trying to think up a good example, but it’s really quite painful, and it doesn’t really make a lot of sense in isolation - it’s only really useful when used with other techniques, and even then it’s takes a lot of boilerplate.

JavaScript Allongé goes into it in some detail, mainly for fun.
The Mostly Adequate Guide to Functional Programming goes into detail on it.

To make it simpler: Ramda can do it out of the box, as can Lodash, both are great libraries.

Note there’s a proposal in the language to add partial application; it’s very early stages (and even if it gets anywhere I suspect the syntax will change), but it can be polyfilled into now if you use Babel to compile your JS code - https://github.com/tc39/proposal-partial-application


Ok Dan,

Cool. All in all it does seem like a potentially nice feature to have, but thank you for the broad based edification. One can of course add such a feature to one’s function to emulate this but I wasn’t sure if with JS it was also built-in.