Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Square brackets around variable name (Javascript) [duplicate]

Can somebody explain, what this line of code represents in Javascript:

const [m, o] = [player.matrix, player.pos]

Im specifically confused by the square brackets around variable names?

like image 891
searchfind Avatar asked Oct 26 '25 22:10

searchfind


1 Answers

This is what we call a destructuring assignment, you are effectively doing this:

const m = player.matrix;
const o = player.pos;

Note that this syntax is part of the ECMAScript 2015 (6th Edition, ECMA-262) standard and is not immediately available to all browser implementations. You can read more about it here.

There is also a compatibility table that you can check.

like image 67
Gorka Hernandez Avatar answered Oct 29 '25 11:10

Gorka Hernandez