6

so i was checking @robbmj 's answer to this question, and i came across the following code:

var timer = duration, minutes, seconds;

and another one here:

var start = Date.now(),
    diff,
    minutes,
    seconds;

I have never been to that code before. Could someone please explain what those two lines of code does or at least how to read them ?

  • E.g. `var timer = duration, minutes = x, seconds = y;` ... `minutes` and `seconds` is declared but w/o any initial values – Asons Apr 23 '18 at 15:18
  • Those are just declarations. `diff`, `minutes`, and `seconds` will still be `undefined`. This can help make `var` declarations more obvious since you will have to worry about hoisting. – zero298 Apr 23 '18 at 15:18
  • the first one set a var timer = duration and then 2 null / undefined vars - minutes and seconds. The second sets start as the current date and then sets three null / undefined vars in the scope. Basically if you follow your variable declaration with a comma, it means you can go directly into declaring another variable in that scope without the need for saying `var` again: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var – Pete Apr 23 '18 at 15:20
  • It's just this code: `var timer = duration; var minutes; var seconds;` condensed a little, `timer` is set to `duration`, `minutes` and `seconds` are undefined. Same thing applies to the second example, where `start` is defined as the value of `Date.now()` and the other variables are "initialized" – Luca Kiebel Apr 23 '18 at 15:20
  • A good references for what is happening is documentation for the [`comma operator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator). A side note though on that style: it came into style before any of the build tools that now minify deliverable code automatically were available. In effect it was used as a way of manually minifying code (no need to write the extra `var x; ` for every declaration). – Jason Cust Apr 23 '18 at 15:23
  • So it was that simple!! Thank you LGSon , zero298 , pete , luca :)) –  Apr 23 '18 at 15:24

4 Answers4

5

You can also use destructuring like this:

let [a, b, c] = [0, 0, 0]
Mohit Gupta
  • 75
  • 1
  • 8
3

You can write

var start = Date.now(),
diff,
minutes,
seconds;

as

var start = Date.now();
var diff;
var minutes;
var seconds;

So you can declare multiple variables in one line as in code snippet 1 where we are initialising start but only declaring other variables.

You can initialise multiple in one line like

var a = 1, b = 2, c = 3;
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
  • @jes Welcome :) – Zohaib Ijaz Apr 23 '18 at 17:31
  • e.g. timer = 1; so `--timer < 0` will be `false`. --timer will first decrement timer and then it will compare as compared to `timer -- < 0` will first compare and then decrement. – Zohaib Ijaz Apr 23 '18 at 17:38
  • yes I just read in here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_(.2B) big thanks to you again :)) –  Apr 23 '18 at 17:40
0

The First line creates 3 variables: timer, minutes and seconds. but is assigning the value of the variable duration to variable timer.

the same story in example 2, just separated with line breaks.

It is a shortened way of saying

var timer = duration;
var minutes;
var seconds;
J-Cake
  • 1,526
  • 1
  • 15
  • 35
0

your second example just separated with line breaks.

define multiple variables on a single line annoying to remove the first or last declaration because they contain the var keyword and semicolon. And every time you add a new declaration, you have to change the semicolon in the old line to a comma.

As a result, define multiple variables on a single line not recommended

using it

var timer = duration;
var minutes = 0;
var seconds = 0;

instead of

var timer = duration, minutes, seconds;
Soroush Chehresa
  • 5,490
  • 1
  • 14
  • 29