50

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ismaël tifous
  • 589
  • 1
  • 5
  • 10
  • 27
    I just love how the question is regarded as "too localized" and it has been seen over 145,000 times. – Jose A Apr 25 '18 at 15:16
  • 7
    This is an important question. I just helped me. It should not be close. – PMA Mar 10 '19 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 https://stackoverflow.com/a/18221979/6068036). – AlexM Jan 25 '21 at 19:03

7 Answers7

51

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • For me it was a ";" after my function close } but your answer helped find it. Installed JSlint and looked for errors – Afshin Ghazi Aug 01 '16 at 10:55
16

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 :)


Nope
  • 22,147
  • 7
  • 47
  • 72
  • 1
    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 ! – Ismaël tifous Mar 21 '13 at 21:36
5

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) {}
Josh McGee
  • 443
  • 6
  • 16
  • 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) – Manchineel Apr 15 '23 at 14:52
0

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.

dwb
  • 2,136
  • 13
  • 27
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
   });
UserOfStackOverFlow
  • 108
  • 1
  • 3
  • 14
  • Nope, pretty sure the highly voted answers got it right with the extra closing brace `}` – phuzi Feb 09 '22 at 15:52
  • 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 ; – UserOfStackOverFlow Feb 09 '22 at 16:10
  • 1
    True but it's not the problem in the question. – phuzi Feb 09 '22 at 16:43
  • Yes, although it was with this problem how I got here. Await for Google indexing. – UserOfStackOverFlow Feb 09 '22 at 16:56
  • 1
    And the error is a fairly generic syntax error – phuzi Feb 09 '22 at 17:03
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()

ttest2727
  • 45
  • 6
-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}));
neuquen
  • 3,991
  • 15
  • 58
  • 78