searchfind
searchfind

Reputation: 57

Square brackets around variable name (Javascript)

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?

Upvotes: 4

Views: 5454

Answers (1)

Gorka Hernandez
Gorka Hernandez

Reputation: 3958

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.

Upvotes: 6

Related Questions