Search code examples
javascriptjslint

Multiple variable declaration in one line - Javascript


I am trying to learn Javascript. I came across the following code.

// Random function.
function order_summary(order_object) {
  var x, y;
  // More code here.
}

When I run this code in Jslint, it's giving me the following error.

Expected ';' and instead saw ','.
var x, y;

I don't see any problems with this code. Can anyone explain what this error means?


Solution

  • Jslint is a sort of a style enforcement tool and doesn't like having multiple variables declared on one line. To fix it, simply declare each variable on each line. e.g.

    var x;
    var y;
    

    The reason why jslint doesn't like this is because javascript has semicolon insertion. So if you accidentally omit a comma like this:

    var x
    y = 10;
    

    JS will insert the semicolon at the end of the first line and you would have accidentally created a global variable y.