jslint - Multiple variable declaration in one line - Javascript - Stack Overflow

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

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?

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?

Share Improve this question asked Jun 28, 2016 at 2:45 PattuPattu 3,8398 gold badges35 silver badges43 bronze badges 5
  • 1 It's not a syntax error, it's a suggested style thing that some people think it is better to declare every variable separately with separate var statements. – nnnnnn Commented Jun 28, 2016 at 2:48
  • Your editor is probably treating lint issues as errors. – acdcjunior Commented Jun 28, 2016 at 2:49
  • 4 Possible duplicate of Expected ';' and instead saw ','. - JSLint multivar setting – Sebastian Simon Commented Jun 28, 2016 at 2:49
  • Jslint has very strong opinions about how javascript code should be written (it's actually Crockford's opinion but Jslint is the one shouting at you). It's not a syntax checker. It's a "things Crockford does not like" checker. Which to be fair, does avoid a lot of potential bugs. Jslint will hurt your feelings - the website warned you already – slebetman Commented Jun 28, 2016 at 2:50
  • You can try using JSHint. It has its own advantages. – Jeffery Tang Commented Jun 28, 2016 at 2:53
Add a ment  | 

3 Answers 3

Reset to default 3

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 ma 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.

There is one very important difference between the two styles:

var l1 = 1,
    l2 = 2;

and

var l1 = 1;
var l2 = 2;

Namely: In the second form the debugger can step through each assignment one-by-one.

In the first form, the entire line is one massive expression and the debugger tries to execute it at once. When you have assignments that depend upon earlier operations, the line-by-line approach is very useful.

It is not a problem at all to declare multiple variables using single var in any javascript run-time. This is just a warning thrown by JSLint as by default it is remending 1 var per line.

If you check the option "multiple vars" under "Tolerate.." this warning will be ignored.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745254675a4618882.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信