Hackernoon logoWhat Does "SyntaxError: Missing formal parameter" Mean? by@evgeniy-lebedev

What Does "SyntaxError: Missing formal parameter" Mean?

image
Evgeniy Lebedev Hacker Noon profile picture

@evgeniy-lebedevEvgeniy Lebedev

Chief Marketing Officer Practicum by Yandex, edtech expert, practicum.yandex.com

You can just declare a function, or you can declare it with the love and care that it deserves.

Let’s say you’re working on a chatbot and you need to write a function that responds to greetings. You want it to respond to the word “Hi” with “Hello!” so you write the following function:

function greet("Hi") {

  let greeting = "Hello!";

  return greeting;

};

Everything seems to be in the right place, but as soon as you try to run your function, the browser prints this error:

❌ SyntaxError: missing formal parameter

Before getting back to our particular case, let's dissect this error a bit. The browser says that the function lacks a formal parameter. This message means that you messed something up during function declaration, or the browser can't quite figure out the arguments you've passed your function.

In our example, we declared a function, but we specified the exact string value of "Hi" as a parameter, instead of a variable. We can't do this because "Hi" is not a variable and the function requires arguments that can accept different possible values. Here's what we should have done:

function greet(userInput) {
  
  let greeting = "Hello!";
    
    if (userInput =="Hi") {
    
    return greeting;

  }
};

How can we fix the SyntaxError: missing formal parameter error?

  1. Check that you've passed all required arguments to the function when calling or declaring it. Sometimes we can overlook this and it can lead to this error.
  2. Check if the function parameters contain anything that's not a variable. If there are such entities, replace them with variables.

For instance, this may look like it will work because we've declared a variable

number
and passed its value, but we actually have to pass the variable name
number
instead:

<script type="text/javascript">

const number = 4;

function multiply(4) {
  return number * number;
}

</script>

Try it yourself

We have some code below that will produce this error. Let's see if you can find and fix it!

const guests = 12;
const adultGuests = 9;

function(12, 9) {
  const numberOfKids = guests - adultGuests;
  const numberOfBottlesToPurchase = adultGuests / 3;
  console.log(`You've got ${numberOfKids} coming to the party, so make sure to get some ice cream. And while you're at it, pick up ${numberOfBottlesToPurchase} bottles of wine.`);
}

Did you find this article useful? You may be interested in learning more about Practicum. We offer online education and mentorship to help you develop a career in tech.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.