1

I am getting some strange behaviour out of JavaScript array.length. In particular, I have an array that returns length as 3, when there is only one element in the array, at index 0. I've read this question/answer dealing with incorrect values returned by array.length, but it doesn't answer my question because my array doesn't seem to be associative.

Here's a screenshot of my console, demonstrating the odd behaviour.

Screenshot Odd Array Behaviour

The code is throwing an error because I'm looping over the first array using array.length and the code is trying to acccess the second and third elements it thinks should be in the array, and not finding them. Other entries in the database seem to not have this problem (see the second array in the screenshot).

So here's the question: Why is array.length in the first array 3 instead of 1?

Community
  • 1
  • 1
Michael.Lumley
  • 2,158
  • 2
  • 27
  • 49
  • could you please provide us with some code? thanks in advance! – King Reload May 02 '17 at 10:28
  • Code is difficult to duplicate. Involves several different modules of the app, as well as a fair bit of asynchronicity. Sorry! Happy to prod the data more from console if there's some particular piece of information that would be useful. – Michael.Lumley May 02 '17 at 10:30
  • can you provide array please? – RRajani May 02 '17 at 10:33
  • 2
    It's just a [sparse array](http://stackoverflow.com/q/1510778/1048572), nothing wrong with it. Something assigned `.length = 3` to it but didn't actually put any values. Or something used `delete`. Without the code that produced this, we cannot tell why this unexpected value occured, but it's definitely a legit value. – Bergi May 02 '17 at 10:34
  • I'm not sure which array you're looking for? – Michael.Lumley May 02 '17 at 10:34
  • But at the moment I don't have a lot of clues how the loop of `array.length` is build. I suppose you loop through it and it has 1 object in it, but instead of 1 it shows 3? – King Reload May 02 '17 at 10:36
  • it's very diificult to answer this without any code. it might be due to the object you are logging to the console getting mutated by some other code before you expand it in dev tools. the object in dev tools console are references, so it could cause confusion – dashton May 02 '17 at 10:43
  • 1
    @Bergi - Thanks. That link helped. If you post it as an answer I will accept it. I also solved my code problem by [iterating properly over my array](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea). – Michael.Lumley May 02 '17 at 10:44

1 Answers1

4

The length Array property in javascript is writable from anyone and is not a reliable source to find the number of elements in the array.

Usually, it is safe to assume that the array has length elements in it, but sometime you can have different behaviours. This one is one of them.

var x = [1,2,3];
x.length = 5;

using a for construct will lead to some undefined values

for (var i = 0; i < x.length; i++) {
    alert(x[i]);
}

using the forEach Array method would result (on Firefox at least) in the desired behaviour.

x.forEach(function(item) {
    alert(item);
});

Note that, if you change the array length to a lesser value than the real value, the array would lose the extra elements and if you restore the original value the extra elements will be lost forever.

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length

m.spyratos
  • 3,014
  • 2
  • 27
  • 35
Eineki
  • 14,008
  • 6
  • 47
  • 54