What Does "SyntaxError: Missing formal parameter" Mean?

Written by evgeniy-lebedev | Published 2021/01/11
Tech Story Tags: syntaxing | js | tutorial | javascript | learning-to-code | coding | programming | exceptions

TLDRvia the TL;DR App

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.

Written by evgeniy-lebedev | Chief Marketing Officer Practicum Coding Bootcamp, edtech expert, practicum.com
Published by HackerNoon on 2021/01/11