Javascript Function Parameters: Positional vs Named.
This is about to be my first actually “educational” post so bare with me, it’s bound to suck, but I’m trying to give (a teeny tiny bit) back to the community so any constructive feedback is very much appreciated.
Let’s imagine we’re building a website/app/just an algorithm for displaying a price on a shopping kart, if you’re trying to modularize your code, you probably have a utility function for this that might look like:
const totalPrice = 5;
const tax = 3;const getPriceWithoutTax = (totalPrice, tax) => totalPrice - tax;getPriceWithoutTax(totalPrice, tax) //Outputs: 2
getPriceWithoutTax(tax, totalPrice) //Outputs: -2 reversed order!
This is a function that takes in Positional Parameters, so that means that our price parameter will always be the first parameter you pass into the function when invoking it and the tax parameter will always be the second.
This is all fine and dandy, in fact, this is the way probably all of us learned how to define functions and parameters, and there’s nothing wrong with it, although you could make an argument that this function does not adhere to functional programming, after all, I’m passing the same parameters but not getting the same output, the order of the parameters in itself has become a third implicit parameter.
What if there was a way to just add those parameters in any order and the function knows which one is which, so that you don’t have to remember the order of them? This is especially useful when you have more than a few of them being passed and also will nudge you to be more consistent with your variable naming.
Let me present: Named Parameters
const totalPrice = 5;
const tax = 3;const getPriceWithoutTax = ({ totalPrice, tax }) => totalPrice-tax;getPriceWithoutTax({ totalPrice, tax }) //Outputs: 2
getPriceWithoutTax({ tax, totalPrice }) //Outputs: 2
A Whole New World from Aladdin OST starts playing
Get Dario Arteaga’s stories in your inbox
Join Medium for free to get updates from this writer.
This way, I can forget about the order, we’re leveraging object shorthands and object destructuring to basically just pass one object into the function with property names that match the ones that are being destructured in the function definition.
But what if the variable I want to pass into the function is already named different than in my function definition, you ask?
Let’s consider a submit function and let me show you the world 🎵
const valid = true;
const password = 'password'const submit = ({ isValid, password}) => {
if (isValid) {
sendThisToSomeAPISomewhereSomehow(password)
}
errorOut('this password is not valid')
}submit({isValid: valid, password}) // Will submit successfully
submit({ password, isValid: valid}) // Still submits successfully
You can just make your differently-named variable, the value of that key, this is something you might encounter if you’re starting to implement this pattern and you’re working on a codebase that hasn’t been doing this, also notice how password can just be a shorthand because that parameter does match exactly
But like I said, a lot of the value of this pattern is nudging you to always name a variable you might be using in several places the same way in order to mantain some kind of rails for your train of thought, something like this was actually what inspired me to write this article; I’m so used to it that I was very confused when I saw a function call take in the parameter (isValid) and when checking the function definition on another file, the parameter was defined as valid
Additional Notes:
You can also make hybrids of this two patterns, I like to stick to all Named Parameters and only have one positional parameter as the last one in case I have a callback or if the function only takes in one parameter. Something like this:
const doThingsAndThen = ({ thing1, thing2 }, cb) => {
sendThings(thing1 + thing2) // There's only one parameter
cb()
}But if you want, make an argument about why some of these are better, this is one I stumbled upon a while back.
// Positional parameters are REQUIRED parameters and named ones are // OPTIONAL onesconst parseText = (text, validationType, {hasMask}, callback) => {
// Validate depending on if it's an email, or a credit card number
// and don't apply a mask unless that's true
}






