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?

2
  • Nope, you'd just be declaring empty variables daR and newB. end will only be equal to endPt(b) Commented Dec 11, 2014 at 0:05
  • No, that explanation does not apply here, it does not have anything to do with the multiple var statement you are experiencing here. You should rather read the other answers. Commented Dec 11, 2014 at 0:14

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

3 Comments

A better example might be to show var end = endPt(b); var daR; var newB;
@Phil -- Yeah - the confusion stems from the first is being assigned a value, the second two aren't.
Wow. Major fail on my part for not recognizing the syntax!! Over-thinking it for sure, thanks for the clarity.
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.

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.