48

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,438
  • 68
  • 55
  • 126
Ismaël tifous
  • 569
  • 1
  • 5
  • 10
  • 25
    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

6 Answers6

50

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
  • 227,407
  • 48
  • 383
  • 507
  • 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
  • 21,907
  • 7
  • 45
  • 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
2

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
  • 353
  • 3
  • 16
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.

dantechguy
  • 1,879
  • 11
  • 26
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
   });
  • 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
-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}));
Keven
  • 3,734
  • 14
  • 54
  • 77