Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the use of , (comma) instead of a proper variable definition like const, var or let, in a chain of declarations, a good JS practice? [duplicate]

i've recently discovered that i can use , (comma) instead of declaring the variable properly like const num1, when in a chain mode, what i want to know is if there is any side effects in using it?

the std way =>

const num1 = 1
const num2 = 2
const num3 = 3

The , way =>

const num1 = 1
, num2 = 2
, num3 = 3

! the console shows no syntax error on both of them !

like image 746
VyTor BrB Avatar asked Sep 18 '25 06:09

VyTor BrB


1 Answers

There's actually no difference, as long as you know that the comma syntax keeps the kind of variable (block scoping, constant) the same all the way through. So this:

const num1 = 1;
const num2 = 2;
const num3 = 3;

Is exactly the same as this:

const num1 = 1,
  num2 = 2,
  num3 = 3;
like image 95
Jack Bashford Avatar answered Sep 20 '25 19:09

Jack Bashford