2

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?

8
  • could you please provide us with some code? thanks in advance! Commented May 2, 2017 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. Commented May 2, 2017 at 10:30
  • can you provide array please? Commented May 2, 2017 at 10:33
  • 2
    It's just a sparse array, 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. Commented May 2, 2017 at 10:34
  • 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. Commented May 2, 2017 at 10:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.