0

CODE DEMO:

See Line 34:

// Tree creation functions
function branch(b) {
    var end = endPt(b), daR, newB;
    ...

(where endPt(b), daR, newB are variables defined in this function, but omitted for simplicity)

What is going on here?

Possible solution: I've read this assets var end equals each of these,

In JavaScript you can use commas to group any number of expressions into a single statement. This is basically an artifact of the for statement, where multiple assignment expressions are often grouped together in the header.

from here:

Does that explanation directly apply here?

Community
  • 1
  • 1
DeBraid
  • 8,751
  • 5
  • 32
  • 43
  • Nope, you'd just be declaring empty variables `daR` and `newB`. `end` will only be equal to `endPt(b)` – Phil Dec 11 '14 at 00:05
  • No, that explanation does not apply here, it does not have anything to do with the [multiple `var` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) you are experiencing here. You should rather read the other answers. – Bergi Dec 11 '14 at 00:14

2 Answers2

2

Pretty simple, you can declare multiple variables inline by separating via comma, so we can break down your example:

function branch(b) {
    var end = endPt(b), //a new variable called "end" is created and assigned the result of endPt with "b" as the parameter
    daR, //an empty variable called "daR" is declared
    newB; //an empty variable called "newB" is declared

As @Phil points out, it's probably easier to separate this statement into:

var end = endPt(b);
var daR, newB;
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • A better example might be to show `var end = endPt(b); var daR; var newB;` – Phil Dec 11 '14 at 00:06
  • @Phil -- Yeah - the confusion stems from the first is being assigned a value, the second two aren't. – tymeJV Dec 11 '14 at 00:06
  • Wow. Major fail on my part for not recognizing the syntax!! Over-thinking it for sure, thanks for the clarity. – DeBraid Dec 11 '14 at 16:31
1

(where endPt(b), daR, newB are variables defined in this function, but omitted for simplicity)

What is going on here?

You are seeing the declaration of the variables end, daR and newB. end also is initialized with the value endPt(b). It is a multiple var statement equivalent to

var end = endPt(b);
var daR;
var newB;

Does that explanation directly apply here?

No, that explanation does not apply here, it does not have anything to do with the construct you have here. You should rather read the other answers on that question.

The "comma expression" explanation would apply to

var end = (endPt(b), daR, newB);

where it would evaluated endPt(b), daR, and then assign the value of newB to end.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375