Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank identifier in Javascript

In golang there is the _ (Blank Identifier).

myValue, _, _ := myFunction()

this way you can omit the 2nd and 3rd return values of a function.

In javascript is it possible to do this?

function myFunction() {
   return [1,2,3]
}

// Something like this
const [first, _, _] = myFunction()
like image 704
Thellimist Avatar asked Dec 21 '19 07:12

Thellimist


Video Answer


1 Answers

When destructuring, unused items can be removed completely (no need to specify a variable name that won't be used later), and unused trailing array items don't even need commas (have the array ] end at the final destructured item you need):

function myFunction() {
   return [1,2,3]
}

const [first] = myFunction()
const [, second] = myFunction()
const [,, third] = myFunction()

console.log(first, second, third);
like image 108
CertainPerformance Avatar answered Sep 22 '22 01:09

CertainPerformance