Console.log(array) Returns Filled Array, But Console.log(array.length) Is 0?
I'm having trouble with an array and I cannot find what exactly is the problem. First a function executes the following two lines in a loop: varArray[overlapCounter] = [a, b, c]; o
Solution 1:
Your problem is that console.log
is not guaranteed to be synchronous; for object references like arrays, when you view the object's property tree you are seeing the tree as it is currently rather than as it was when you called log
. Integers, on the other hand, are passed by value and thus when you log(array.length)
you get the value of the length at the very instant log
was called.
If you want to print the array as it as when you called log
, you should first make a copy of it using array.slice()
(note that this only creates a shallow copy of the array):
console.log(array.slice())
Post a Comment for "Console.log(array) Returns Filled Array, But Console.log(array.length) Is 0?"