Uncaught SyntaxError: Unexpected token var

88,386

Solution 1

Count the open parentheses on this line:

loadarea.empty( ).append($.thumbnailviewer2.buildimage($, $anchor, s, options)
              ^ ^       ^                             ^                      ^
              1 0       1                             2                      1

Add another closing paren; the parser thinks you're still specifying arguments to the append() function, and the var keyword is invalid in this context.

Also, use semicolons. If not for your sake, do it for Douglas' health.

Solution 2

I had a similar error message in the console with the minifier parsing my javascript source code. I found that using // comments like so always interrupted the minification process, and gave me an error in the console. Therefore i switched all /* comments */ like so. MDN Javascript Comments And immediately everything parsed as expected. Hope it helps.

Share:
88,386

Related videos on Youtube

Bob R
Author by

Bob R

Updated on September 04, 2020

Comments

  • Bob R
    Bob R over 3 years

    I have an error Uncaught SyntaxError: Unexpected token var displayed between (1) and (2) its a very odd error and it doesn't make sense at all.

    if ($hiddenimage.length==0) { //if this is the first time moving over or clicking on the anchor link
        var $hiddenimage=$('<img src="'+this.href+'" />').appendTo($hiddenimagediv) //populate hidden div with enlarged image
        $hiddenimage.bind('loadevt', function(e){ //when enlarged image has fully loaded
            loadarea.empty().append($.thumbnailviewer2.buildimage($, $anchor, s, options)
            (1) - var $targetimage=$.thumbnailviewer2.buildimage($, $anchor, s, options) //create reference actual enlarged image
            (2) - $loadarea.empty().append($targetimage) //show enlarged image
            $.thumbnailviewer2.showimage($targetimage, s)
        })
    
    • Black Mamba
      Black Mamba about 7 years
      The error is due to an unclosed parentheses .
  • Bob R
    Bob R over 13 years
    Thanks a lot I cant believe it was that easy, I didn't know you could use semicolons LOL
  • Michael Lorton
    Michael Lorton over 13 years
    Use semicolons, and use an editor that will show you matching parens.
  • andromeda
    andromeda almost 2 years
    This is a big problem and can cause code in production not run at all.
  • mangrove108
    mangrove108 almost 2 years
    Can't verify for production, may very well be. Voted on cdhowie's answer 👍

Related