Volver

JSBits #2: Return more than one value

Diario del capitán, fecha estelar d67.y38/AB

Javascript ES6 Development
JSBits #2: Return more than one value

Here's another JavaScript bits we wanted to share with you. Small tips and tricks for JavaScript developers for all levels. This time around, we share how to return more than one value.

Some programming languages implement the tuple data structure. It's (normally) a fixed length, immutable list, that has very good performance.

Tuples are used lot of times to return more than one value from a function.

It's a common and idiomatic pattern in Javascript to use Array destructuring for the same purpose:

function calculate(...) {
  ...
  // return the values wrapped into an array
  return [total, average];
}

function myBussinesLogic(...) {
  // destructure the array to obtain the values
  const [total, average] = calculate(...);
}

Happy destructuring!

Compartir este post

Artículos relacionados

JSBits #4: Comparison operator

JSBits #4: Comparison operator

Here's another JavaScript bits we wanted to share with you. Let's talk about the comparison operator, today.

Leer el artículo
JSBits #5: Loop statements: 'for..in' vs 'for..of'

JSBits #5: Loop statements: 'for..in' vs 'for..of'

Here's another JavaScript bits we wanted to share with you. Let's talk about the loop statements, today.

Leer el artículo
JSBits #6: Lerna vs. Yarn workspaces

JSBits #6: Lerna vs. Yarn workspaces

Here's another JavaScript bits we wanted to share with you. Let's talk about Lerna vs. Yarn workspaces, today.

Leer el artículo