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) }
Yet, my browser (firefox) doesn't accept that. Instead I have to go like so:
var b while (b=a.pop()) { do_sth(b) }
which works. Is this behaviour expected?
Yes, it is.
If you want to, you can use a for loop, like this:
for
for (var b; b = a.pop(); ) { //Note the final semicolon do_sth(b); }
2.1m questions
2.1m answers
60 comments
57.0k users