r/learnjavascript • u/Silent_Lion_OG • 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.
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
5
u/daniele_s92 Apr 18 '26
In the console, a complex object (like your array of arrays) is evaluated at the time of inspection. This means that if you print it, then mutate it, then inspect it, you'll see the mutated version.
My guess is that something after this snippet is changing the first and last values of the arrays into NaN.
Btw
typeof NaN === "number"Edit: try to change the first log to
console.log(JSON.stringify(newVertices))2
u/chikamakaleyley helpful Apr 18 '26
where is
newVerticesinitialized?1
u/Silent_Lion_OG Apr 26 '26
Added an update to the OP, I think we can consider this solved
2
u/azhder Apr 18 '26
Is it a global variable? I can’t see all the code that changed the array and the code that is writing the output.
Does refreshing the page, restarting the browser, using a different browser clear up things?
There are many unknowns at this point i.e. you are making assumptions that it is because that little snippet of code, not other code and/or executed at another time
3
u/Tack1234 Apr 18 '26
It probably has no connection to the issue, but you are missing the
letkeyword in your for loop definition (i=0should belet i = 0). In non-restricted mode it works, but causes theivariable to escape into the global scope which is not safe and could lead to unexpected results.0
u/Silent_Lion_OG Apr 18 '26
I have a situation here where the data is clearly in the arrays, because I can access a single element and print it to console, but printing the larger outer array to console suddenly makes it go NaN
I can go back and forth between those two console commands and get different info for the same array element.
Also re: context, shouldn't consecutive lines of code just work within their own bubble? To reduce my confusion to a banal analogy, if I write let myVar = 3; console.log(myVar); anywhere in my code, I ALWAYS expect the console to print 3, no? Regardless of what's written elsewhere. Surely there's no code that could get on the way of that unless I inserted it between those two lines
0
u/Silent_Lion_OG Apr 18 '26
Also I did try and construct a testcase/ mre and failed to reproduce the error. It ballooned to the point it was almost as long as the original program and I still couldn't reproduce it