Y
Y
yankoyski2018-03-02 20:59:10
JavaScript
yankoyski, 2018-03-02 20:59:10

What is the difference between these options?

What is the difference between these options: and
var tempScrollTop, currentScrollTop = 0;

var tempScrollTop = 0;
var currentScrollTop = 0;

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Sokolov, 2018-03-02
@yankoyski

The first one is bad because two variables are declared on the same line.
The second is bad because two var.
I would like this (now they will downvote .. Oh, no, you can’t downvote here :)

var tempScrollTop
  , currentScrollTop = 0
;

A comma at the beginning will confuse some, but this way you can safely delete a line without fear of messing with a comma.
Upd. The main difference is that in the 1st variant the variable tempScrollTopremains uninitialized, its value is undefined, and in the 2nd variant it is given the initial value 0.

D
Deodatuss, 2018-03-02
@Deodatuss

in that the normal, supported, less dangerous option is the second, and the first should be used only when there are no values ​​for variables, such as:
var tempScrollTop, currentScrollTop;

S
string15, 2018-03-02
@string15

Readability

D
dom1n1k, 2018-03-03
@dom1n1k

The difference is mainly aesthetic.
I am for the second option - it is more reliable, more readable and does not depend on the size of the indent.
The exception is variables of the same type - it is normal to put them in one line:
let x1, y1, x2, y2;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question