Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaration for variable in while condition in javascript

Tags:

javascript

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?

like image 299
René Nyffenegger Avatar asked Nov 10 '09 21:11

René Nyffenegger


People also ask

Can you declare a variable in a while loop JavaScript?

JavaScript doesn't have block scope. So all var declarations are at function scope. So declaring a variable in a while expression doesn't make sense in JavaScript. Additionally, you should end your statements with a semicolon.

Can you declare a variable in a while loop condition?

Yes. you can declare a variable inside any loop(includes do while loop.

Can you declare a variable in an if statement JavaScript?

Should you define a variable inside IF statement? Honestly, there's no right or wrong answer to this question. JavaScript allows it, so you can make your decision from there.

Can we use while loop in JavaScript?

The do while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.


2 Answers

Yes, it is.

If you want to, you can use a for loop, like this:

for (var b; b = a.pop(); ) {      //Note the final semicolon     do_sth(b); } 
like image 101
SLaks Avatar answered Oct 08 '22 17:10

SLaks


The question is a little dated, but I think the answers all miss an important distinction. That is, a while loop expects an expression that evaluates to a conditional, i.e., a boolean or value that can be converted to a boolean. See Mozilla docs for details.

A pure assignment (without instantiation) is coerced to a boolean via its default return value (the value of the right-hand-side).

A var (or let or const) is a statement that allows an optional assignment but has a return value of undefined.

You can easily test this in your console:

var foo = 42; // undefined bar = 42      // 42 

The return values alone don't answer the question, since undefined is falsey, but does show that even if JS let you put a var in a conditional it would simply always evaluate to false.

Others have mentioned for statements and that they allow declaration and instantiation of variables. This is true, but the documentation explains that for expects a statement or assigment.

Opinions may vary, but for me all this adds up to an understandable consistency not a quirk in behavior with regard to loops. A while loop is better thought of as a looping version of an if statement than akin to a for loop. If there is quirkiness in all of this, it's the for statement's wholesale divergence from the language's normal syntax.

like image 23
ballenf Avatar answered Oct 08 '22 16:10

ballenf