javascriptdestructuringvariable-declaration

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?


Solution

  • 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.