JavaScript relies on brackets to know where function calls start and end. If you miss out a piece of syntax before a function is closed, you’ll encounter the “SyntaxError: missing ) after argument list” error.
This guide explores what this error means and why it is raised. We’ll walk through an example of this issue so you can learn what you need to know to fix the problem.
SyntaxError: missing ) after argument list
The “missing ) after argument list” message tells us that there is a syntax error inside a function call.
This may happen if you add a comma at the end of a list of arguments that is not followed by another argument.
JavaScript expects another argument after each comma. If JavaScript cannot find another argument, your code cannot be parsed successfully and the closing parenthesis will raise a syntax error.
Another potential cause for this solution is enclosing the comma you need to separate arguments in a function inside a string.
If you encounter this error, carefully read over all the syntax in the code that the JavaScript error points to. Make sure all your brackets match up and you have used commas correctly inside your function call.
An Example Scenario
We’re going to write a program that calculates whether a student has passed or failed a test at school. To start, let’s define the grade a student has earned and a message we’ll print to the console informing us of whether a student has passed or failed:
var grade = 57; var message = "This student has X their test.";
The JavaScript variable “message” includes a placeholder letter X. We’ll replace this with “passed” or “failed” later in our program.
If a student’s grade is over 53, they have passed the test. Otherwise, they have failed.
Now that we have defined our student’s grade, we can calculate whether they have passed or failed their test. To do so, we’re going to use an if statement:
if (grade > 53) { message.replace("X," "passed"); } else { message.replace("X", "failed"); }
We use one if statement and one else statement to evaluate whether a student has passed or failed. If the if
statement evaluates to true, the value of “X” in our “message” string becomes “passed”; otherwise, the value of “X” becomes “failed”.
Now that we’ve written the message informing us of whether a student has passed or failed their test, we can print that message to the JavaScript console:
console.log(message);
Let’s run our code and see what happens:
Uncaught SyntaxError: missing ) after argument list
Our code returns a syntax error.
The Solution
JavaScript is unable to evaluate our code because we have made a mistake inside one of our function calls. We know this because argument lists exist in function calls.
Let’s take a look at our function calls:
message.replace("X," "passed"); message.replace("X", "failed");
While the second statement appears to be syntactically correct, the first one contains an error. We have added a comma inside our first string instead of after our first string. Notice the “X,” statement in the first replace()
call.
This causes our argument list to contain two values that appear after one another. This is incorrect syntax. Arguments must be separated with a comma.
To fix this error, we’re going to move the comma from inside the “X,” string to outside the string:
message.replace("X", "passed");
We’ve moved the comma outside of the string. Let’s run our code and see if it works.
Our code returns:
"This student has passed their test."
Our code has executed successfully!
Conclusion
The “SyntaxError: missing ) after argument list” error is raised if a function call cannot be evaluated correctly. To fix this error, make sure your arguments are formatted correctly. Double-check that all of the arguments in the function call are separated by commas.
Now you have the knowledge you need to fix this syntax error like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.