54

I am getting the error:

SyntaxError: missing ) after argument list

With this javascript:

var nav = document.getElementsByClassName('nav-coll');
for (var i = 0; i < button.length; i++) {
    nav[i].addEventListener('click',function(){
            console.log('haha');
        }
    }, false);
};

What does this error mean?

3
  • 28
    I just love how the question is regarded as "too localized" and it has been seen over 145,000 times. Commented Apr 25, 2018 at 15:16
  • 8
    This is an important question. I just helped me. It should not be close. Commented Mar 10, 2019 at 23:15
  • For me, I had a function call where I put the function's inputs on the next line. Automatic semicolon injection then caused issues (see stackoverflow.com/a/18221979/6068036). Commented Jan 25, 2021 at 19:03

7 Answers 7

54

You have an extra closing } in your function.

var nav = document.getElementsByClassName('nav-coll');
for (var i = 0; i < button.length; i++) {
    nav[i].addEventListener('click',function(){
            console.log('haha');
        }        // <== remove this brace
    }, false);
};

You really should be using something like JSHint or JSLint to help find these things. These tools integrate with many editors and IDEs, or you can just paste a code fragment into the above web sites and ask for an analysis.

Sign up to request clarification or add additional context in comments.

1 Comment

For me it was a ";" after my function close } but your answer helped find it. Installed JSlint and looked for errors
17

You got an extra } to many as seen below:

var nav = document.getElementsByClassName('nav-coll');
for (var i = 0; i < button.length; i++) {
    nav[i].addEventListener('click',function(){
            console.log('haha');
        } // <-- REMOVE THIS :)
    }, false);
};

A very good tool for those things is jsFiddle. I have created a fiddle with your invalid code and when clicking the TidyUp button it formats your code which makes it clearer if there are any possible mistakes with missing braces.


DEMO - Your code in a fiddle, have a play :)


1 Comment

wow...i'm sorry it was a really stupid mistake, i use SublimeText 2 but i just couldn't see it, i think i'm going to sleep...Thanks a lot !
9

just posting in case anyone else has the same error...

I was using 'await' outside of an 'async' function and for whatever reason that results in a 'missing ) after argument list' error.

The solution was to make the function asynchronous

function functionName(args) {}

becomes

async function functionName(args) {}

2 Comments

I'm doing the same exact thing and getting the same exact error, except I did use the async keyword (in fact whether I use it or not does not change a thing)
Depending on the position of the await, you may get the correct error ("Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules"), or the somewhat less helpful "Uncaught SyntaxError: missing ) after argument list". Yeah, at least we now know yet another "fun" trick question about JavaScript.
3

Similar to Josh McGee, I was trying to use await in the global scope, which, since it isn't an async function, will throw an error. See here for solutions.

Comments

0

This error maybe is the version of your Android (especially if you are using Web Views), try to change your code like an old version of the JavaScript Engine, like:

   .then(function(canvas){
            //canvas object can gained here
   });

5 Comments

Nope, pretty sure the highly voted answers got it right with the extra closing brace }
The point here is that if you try to use lambda .then(canvas => { //code canvas obj }); the compiler throws this error, altrought it not have any relation with "missing arguments" (if you're using old Android version, where Chrome use a old version of JavaScript engine). Probably in the epoch the compiler was not optimized/fixed to show correct error messages ;
True but it's not the problem in the question.
Yes, although it was with this problem how I got here. Await for Google indexing.
And the error is a fairly generic syntax error
0

This error can also occur if you are missing a (tick) '

Failed:

            if (!string.IsNullOrWhiteSpace(clientSideMethod))
            {
                return "function(e){" + clientSideMethod + " OnOptionChangedDxGrid(e," + hasConditionalFormatting.ToString().ToLower() + "','" + "false".ToString().ToLower() + "', '" + string.Empty + "'); }";
            }
            else
            {
                return "function(e){ OnOptionChangedDxGrid(e," + hasConditionalFormatting.ToString().ToLower() + "','" + "false".ToString().ToLower() + "', '" + string.Empty + "'); }";
            }

Worked:

        public static string GetOnOptionChangedMethodCall(string clientSideMethod, bool hasConditionalFormatting)
        {
            if (!string.IsNullOrWhiteSpace(clientSideMethod))
            {
                return "function(e){" + clientSideMethod + " OnOptionChangedDxGrid(e,'" + hasConditionalFormatting.ToString().ToLower() + "','" + "false".ToString().ToLower() + "', '" + string.Empty + "'); }";
            }
            else
            {
                return "function(e){ OnOptionChangedDxGrid(e,'" + hasConditionalFormatting.ToString().ToLower() + "','" + "false".ToString().ToLower() + "', '" + string.Empty + "'); }";
            }
        }

Notice there is a missing (tick) ` before the first double quote: (e," + hasConditionalFormatting.ToString()

Comments

-1

I got the same error and I figured with the increased use of ES6 and string interpolation, that more and more people would start making the same mistake I did with Template Literals:

Initially, I logged a statement like so:

console.log(`Metadata: ${data}`);

But then changed it and forgot to remove the ${}:

console.log('Metadata: ' + JSON.stringify(${data}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.