Do function parameter names not matter at all

Say you have a function

function myFunction() {
  // some code
}

And another function with a variable calling the myFunction

function anotherFunction {
    var myVar = "Something";
    myFunction(myVar);
}

Now updating the first function below, it would seem that I can use virtually any word to get the same data. What is the thought behind this? Are we just supposed to keep track and keep everything in sequential order when you have more than one parameter or argument being sent to a function?

function myFunction(WhateverWordCanGoHere) {
  // some code
  alert(WhateverWordCanGoHere);
}

As you can see the variable myVar is being referenced as WhateverWordCanGoHere. This doesn’t seem to make much sense.

If you add pass another variable to the function myFunction, then whatever the second word is works.

Can someone explain this logic? It just seems that the word doesn’t matter but the amount of comma-separated words and their sequential order do.

I was looking for positional arguments and named arguments.

Check out unpacking object properties using destructuring in the mozilla es6 docs.

The benefit of function parameter names is that it doesn’t matter which variable name is used when calling the function. The benefit being that regardless of what’s happening outside, it’s always the same parameter name on the inside of the function that’s used.

You can have several different variable names, and use all of them to call a function.

const appleCount = 4;
const orangeCount = 2;
const pearCount = 5;

const orangesAndApplesCount = add(appleCount, orangeCount);
const totalFruitCount = add(orangesAndApplesCount, pearCount);

Outside of the function, those variable names can be anything at all.

Inside of the function though it’s always the same consistent parameter names that are used.

function add(fingers, toes) {
  return fingers + toes;
}

No matter the types of fruit that you may have, you can always use your fingers and toes to count them up. :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.