2

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 !

Lelio Faieta
  • 6,242
  • 6
  • 38
  • 64
VyTor BrB
  • 43
  • 5
  • There's no difference between them. All the declaration statements allow you to declare multiple variables, separating them with comma. – Barmar May 08 '19 at 23:53
  • Note that the second example is equivalent to `const num1 = 1, num2 = 2, num3 = 3;`. It is just formatted on multiple lines. Usually when formatting like this, the comma is at the end of each line instead of the beginning. – Code-Apprentice May 08 '19 at 23:57

3 Answers3

2

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;
Jack Bashford
  • 40,575
  • 10
  • 44
  • 74
2

It is totally equivalent same.

You can refer Declaring and initializing two variables

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

Alice
  • 77
  • 3
  • 4
0

Note that the second example is equivalent to

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

This makes it clear that you are just declaring a list of variables separated with a comma. Traditionally, this is formatted as

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

Note that here the comma is at the end of the line. You just happened to stumble on this a slightly different form of this same thing.

Lelio Faieta
  • 6,242
  • 6
  • 38
  • 64
Code-Apprentice
  • 76,639
  • 19
  • 130
  • 241