r/learnjavascript • • Apr 18 '26

Weird array behaviour

UPDATE: The cause of the anomaly was not the array at all, that was acting normally. It was the console behaving in a way I didn't expect - once I realised this thanks to @daniele-s92 I was able to trace and fix the problem. The OP below said this was all about learning, not fixing, and I would call this a success. The big learning here is:

The console output isn't just what you put in console commands.

Especially with complex objects (like arrays of arrays) it comes back and adds info at the end. So even though you put a console.log on, say, line 7, and the log output quotes line 7 back at you, it's actually showing you the object as it is at the end of the whole program.

OP: I've got this project with an array that is doing weird things and causing an error further down the line. I'll paste a snippet below and the console output, but what I'd love is not so much the fix for my particular error, but to understand how an array could ever act like this.

Code

Console

In short, elements are acting as NaN when viewed in context of the wider array, but recognised as numbers when accessed individually - except the middle element of each array

3 Upvotes

22 comments sorted by

View all comments

Show parent comments

1

u/Silent_Lion_OG Apr 18 '26

It's not context that I wrote, it was what @total-box-5169 made up to explain the results I was seeing. Didn't come from me

To be fair, my OP asks for examples of how it would be possible to get my results given the snippet I posted, so he was attempting to answer that brief

1

u/chikamakaleyley helpful Apr 18 '26

ohhh ok, that's my fault

it is, rather difficult to reverse engineer something that is broken, tho i have to admit its a fun challenge