-1

let arr = [1,2,3]
let [a,b,c] = arr; <= How this type of initialization of variables is called

mitkoik
  • 17
  • 1
  • 5

1 Answers1

1

This is called destructuring assignment in JavaScript.

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

In your example let [a,b,c] = arr;, you are unpacking the source object arr, and assigning them to 3 variables a, b and c.

We already saw one example of destructuring assignment on an array above.

The general form of the syntax is:

[ variable1, variable2, ..., variableN ] = array;

This will just assign variable1 through variableN to the corresponding item in the array. If you want to declare your variables at the same time, you can add a var, let, or const in front of the assignment:

var [ variable1, variable2, ..., variableN ] = array;
let [ variable1, variable2, ..., variableN ] = array;
const [ variable1, variable2, ..., variableN ] = array;
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317