I have an array within a JavaScript object that is behaving in a manner I cannot explain.
The code is:
var board = {
val: [false],
init: function() {
console.log(board); // when expanded, console says board.val[0] = true
console.log(board.val); // console says board.val[0] = false
board.val[0] = true;
},
};
board.init();
The method outputs the object in the console, THEN modifies the value in the array. But the output of the object before the value has been changed indicates the value has already been changed.
I am looking for an explanation of why this is happening, or a link to an explanation. Please ask for clarification in the comments if any is needed. I will be happy to give any details I've neglected.
The array in the project I am working on is a large, multi-dimensional array, and this behavior is causing unexpected results. I'm sorry I haven't got any ideas or research to share. I don't know what to search for that doesn't give me the basics of JavaScript arrays. I thought I knew how JavaScript arrays and objects work, but this is stumping me.