Relative Content

Tag Archive for javascriptarraysecmascript-6

Javascripts Array.fill() Peculiar Behaviour [duplicate]

This question already has answers here: Changing one cell in multidimensional array updates entire column (1 answer) Array.fill gives same object repeated. why? [duplicate] (2 answers) Array.prototype.fill() with object passes reference and not new instance (7 answers) Closed 1 hour ago. Why is it that the output so weird! What is the fill method actually […]

Javascripts Array.fill() Peculiar Behaviour! why does

let matrix = new Array(3).fill(new Array(3).fill(0)); // creates a 2d Array and fills them with 0 matrix[1][1] = 5; // say console.log(matrix); // should output [[0, 0, 0],[0, 5, 0],[0, 0, 0] // actual-output: [[0, 5, 0], [0, 5, 0], [0, 5, 0]] Why is it the output so weird! what is the fill method […]