0

Can someone please explain the below code for me?

  1. Is the line setting multiple variables at the same time? If so, what is are they?
  2. If it is setting multiple variables, I would have expected all the items to be X=Y, but instead I get random variables within the line, like reevVal, mmareev, amareev, fvVal.

Any help is greatly appreciated.

var balance = $(".input-balance"), value = $(".balance-value"), reevVal, mmaVal = $(".mma-val span"), mmareev, amaVal = $(".ama-val span"), amareev, triggerPlus = "false", triggerMinus ="false", fvVal;
Lefty
  • 535
  • 6
  • 20
  • Multiple variable definition, the vars witout value will be setted as undefined – David Sep 09 '21 at 17:42
  • Is there an easy way to namespace all the variables? for example Helper.Slider.balance, Helper.Slider.value, or do I need to define each separately? – Lefty Sep 09 '21 at 17:45
  • Need more explanation, from you code I understand only that you define 10 variables comma separated, some with value, some empty (with undefined value) – CuteShaun Sep 09 '21 at 17:46
  • 1
    @Lefty Create an object? – VLAZ Sep 09 '21 at 17:46
  • Listen to @VLAZ and create an object with 10 different properties, it will be more convenient to you – CuteShaun Sep 09 '21 at 17:49

1 Answers1

1
var x = 0, y, z = 1

is equivalent to

var x = 0;
var y; // undefined
var z = 1

There's also the similar, but different, comma operator that lets you "group" up multiple expressions into a single expression. The value of the last sub-expression is returned, but all sub-expressions are evaluated:

let x = (5, doSix(), 7); // x = 7
x = 8, doNine(), 10; // 10

Note the parens are necessary in the 1st line above to make it clear it's a single declaration and not a list of declarations like in the 1st example. The parens aren't necessary in the 2nd line, since there's not let|var|const so it's not a declaration, but an assignment.

In practice, it's most common in for loops where you're constrained to a single expression:

for (let i = 0, j = 10; i < j; i++, j--)
  console.log(i, j);
junvar
  • 11,151
  • 2
  • 30
  • 46