declaration for variable in while condition in javascript

前端 未结 4 951
别跟我提以往
别跟我提以往 2020-12-28 12:57

In essence, I am trying to declare a variable in the condition-part of a while loop in javascript:

while (var b=a.pop()) {
  do_sth(b)
}

Ye

4条回答
  •  天涯浪人
    2020-12-28 13:45

    JavaScript does not have block scope. It has function scope. So to make sure that humans and JavaScript both read the code the same way, you should manually hoist your var declarations right up to the top of functions.

    Here's what JSLint says about your code:

    Problem at line 1 character 8: Expected an identifier and instead saw 'var'.
    

    Use JSLint, at least while you're learning JavaScript. You'll learn a lot very quickly. It will hurt your feelings.

提交回复
热议问题