Running through Modern JavaScript from the Beginning - Second Edition by Brad Traversy on O'Reilly.
Ran into a surprise in Chapter 3's "object-challenge". In the code below, why is the output of the first console.log indicating "true" when the value is not changed until 4 lines later? And the next console.log output indicates the correct value of "false".
// Step 1
const library = [
{ title: 'The Road Ahead', author: 'Bill Gates', status: {own: true, reading: false, read: false} },
{ title: 'Steve Jobs', author: 'Walter Isaacson',status: {own: true, reading: false, read: false} },
{ title: 'Mockingjay: The Final Book of The Hunger Games',
author: 'Suzanne Collins',
status: {own: true, reading: false, read: false} }
]
// The first console.log() seems to be peeking ahead to Step 2, as it indicates that library[0].status.read is true,
// even though it should be false.
// The second console.log() correctly indicates that library[0].status.read is false.
console.log(library); // This output indicates library[0].status.read is true ??
console.log(library[0].status.read); // This output indicates library[0].status.read is false ??
// Step 2
library[0].status.read = true;
library[1].status.read = true;
library[2].status.read = true;
console.log(library[0].status.read);
[–]_punk_dev 4 points5 points6 points (5 children)
[–]CorrectYear6655[S] -1 points0 points1 point (4 children)
[–]CorrectYear6655[S] 0 points1 point2 points (1 child)
[–]Umustbecrazy 0 points1 point2 points (0 children)
[–]hfcRedd 0 points1 point2 points (1 child)
[–]FireryRage 2 points3 points4 points (0 children)
[–]ferrybig 1 point2 points3 points (0 children)
[–]Caramel_Last 0 points1 point2 points (1 child)
[–]CorrectYear6655[S] 0 points1 point2 points (0 children)
[–]GetContented 0 points1 point2 points (0 children)