Does the Destructuring array get a second value?

How to get an array using destruction?

const num = [1,2,3,4,5];
const [ first ] = num; //1

console.log(first)I can get 1, but when I try to do it const [ null, second ] = num, he was expecting a token error. How to get the 2nd element of the num array?

+9
source share
2 answers

You can skip the parameter name, just a comma

var num = [1, 2, 3, 4, 5];
var [ ,x] = num;

for more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Ignoring_some_returned_values section Ignoring some returned values

+17
source

, :

var {1: second} = num;

, , .

. ?

+8

Source: https://habr.com/ru/post/1679315/


All Articles