Search code examples
javascriptarraystypescriptjavascript-objects

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()

Solution

  • 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);