0

Today I came across the following JS:

    for (var b = k, d = function (c) {
       // function body
    }, a = b.p, e = z; 0 < c;) {

    } // for loop closing cause

Can some please explain what's happening here? I'm confused about the function, normal "for" loop would do:

    for(x=0; x<something; x++){

    }

This loops appears to follow a different structure:

    for(x=0; d = function(c), a = somevar, e = somevar, 0 < c)

Why are there 5 elements in the for loop? Thanks!

1
  • for loops have three parts: for (initialization; condition; increment). Any of these parts can have zero or more statements, ie: for (;;) is valid and so is for (a=1,b=2,c=3; a!=10; ) Commented Oct 28, 2012 at 8:13

6 Answers 6

1

for (var b = k, d = function (c) {}, a = b.p, e = z; 0 < c;) {

} // for loop closing cause

for(x=0; x<something; x++)

If you map the first for loop with the signature for loop...

The code in bold is the declaration part for different variables that are going to be used inside the for loop.. .. Any number of variables can be declared here...

The second is the check condition...

And the third part is empty

Sign up to request clarification or add additional context in comments.

Comments

1

There are not five parts, only three:

  • The first one contains four variable declarations (b, d, a and e).
  • The second one contains a comparison (0 < c).
  • The third one is empty.

Each part is separated by semicolons (;) and the variable declarations are separated by commas ,.

The first part is allowed to contain either an expression or a variable declaration list. For more information, have a look at the specification or the MDN JavaScript documentation:

initialization
An expression (including assignment expressions) or variable declaration. Typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword. These variables are not local to the loop, i.e. they are in the same scope the for loop is in. The result of this expression is discarded.


Only because for(x=0; x<something; x++) is the most typical form, does not mean others don't exist. Each part can contain an arbitrary expression. For example, a useful way to iterate over all children of a DOM element is the following:

for(var node = element.firstChild; node; node = node.nextSibling) {

}

First, node is initialised with the first child of the element. The second part just tests whether node is not null (you could also write node !== null explicitly) and the third part (executed after the iteration) assigns the next child to node.

Comments

0

Ill explain, but basically this is not good code.

Its not good code because you need to go online just to figure it out.

for (var b = k, d = function (c) { // function body }, a = b.p, e = z; 0 < c;) {

} // for loop closing cause

is the equivalent of

var b = k,
    d = function (c) {
      // function body
    },
    a = b.p,
    e = z;

for (; 0 < c; ){

}

for loop parents has three parts 1. Initialization 2. Condition 3. Ending statement

That must all be separated by a ;

except these are all optional in a for loop

for (var init = 0; init < 10; init += 1) {}

same as:

var init = 0;
for (;;) {
 if (!(init < 10)) {
  break;
 }
 init += 10;
}

Comments

0

The control variables in a loop can also be a valid expressions as you have in your example.

Comments

0

The first loop you posted is slightly different from the last. The first declares and assigns several variables (one of which is a function), then provides a condition (0<c), then does nothing every iteration. The last seems invalid.

The problem with the first one seems to be only that it does not intialize a c, so unless c is a variable from outside of the scope of the loop that is somehow being changed inside its body, the loop will either not run at all (if c>=0) or it will run forever (if c is indeed less than 0).

Comments

0
 for (var b = k, d = function (c) {
       // function body
    }, a = b.p, e = z; 0 < c;) {

    } // for loop closing cause

as long as d is not 0 the for loop will run. d is 0 when 0 < c

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.