How do Trailing Commas work in JavaScript?

2

When using methods / functions in JavaScript, as in other programming languages we can pass parameters

EXAMPLE

function saludar(name){
   console.log("Hola: "+name)
}

Here we send the function call

saludar("SO en español")

The result will be

"Hola SO en español"

EXAMPLE 2

function hi(name, email, ){
  console.log(name+email)
}
  

At the moment of calling the function, as in the following line, it would give   error then with the comma that is of more mark error to be   waiting for an extra marámetro

hi("alfa", "[email protected]")

EXCEPTION TO THE RULE

Although the error occurs for the functions, for the case of the arrangements the use of extra commas has been allowed

EXAMPLE

let datos = [1, 2, 3, 4, 54,];

console.log(datos);

How does this treat the trailing commas ?

    
asked by element 08.10.2018 в 04:26
source

2 answers

1

How does it work?

From the proposed new standard they are ignored in the declaration of function parameters, and other standards did the same for objects (ES5) and arrays.

What's the use?

Unify the format of the code and facilitate the revision and diffs.

Example of original code without using trailing:

function laSuperFuntion(
  param1,
  param2
) {
   return ( param1 +  param2);
}

Add the option to subtract:

function laSuperFuntion(
  param1,
  param2,
  param3
) {
   return 'restar'!=param3?(param1 + param2):(param1 - param2);
}

A version control will say:

function laSuperFuntion(
  param1,
  param2,  // <- modificado
  param3   // <- nuevo
) {
   return 'restar'!=param3?(param1 + param2):(param1 - param2);  // <- modificado
}

If instead we have the possibility of using trailing commas in the functions ...

Example of original code with trailing:

function laSuperFuntion(
  param1,
  param2,
) {
   return ( param1 +  param2);
}

Add the option to subtract:

function laSuperFuntion(
  param1,
  param2,
  param3
) {
   return 'restar'!=param3?(param1 + param2):(param1 - param2);
}

A version control will say something closer to what happened:

(a parameter was added and the result modified)

function laSuperFuntion(
  param1,
  param2,
  param3   // <- nuevo
) {
   return 'restar'!=param3?(param1 + param2):(param1 - param2);  // <- modificado
}
    
answered by 09.10.2018 / 02:13
source
2

JavaScript has included this somewhat permissive touch to not consider as a mistake a coma of more when passing parameters in a function as follows

EXAMPLE 1

let saludo = (name, ) => {
  console.log(name)
}

saludo("alfredo")

In the previous example in an arrow function associated with a variable, the by expression mode will also work

EXAMPLE 2

let saludo = function(name, ){
  console.log(name)
}

saludo("alfa")

At the level of a function by expression, that is, an anonymous function assigned to a variable

EXAMPLE 3

function saludo(name, ){
  console.log(name)
}

saludo("alfa")

At the level of a function per declaration, that is, a function that has a direct name assigned

IN ALL PREVIOUS CASES IT WILL WORK

    
answered by 08.10.2018 в 04:26