4

See below given scenario

var x = [];
x['abc'] = "hello";
console.log(x.length); //returns 0

var x = [1,2];
x['abc'] = "hello";
console.log(x.length); //returns 2

Any reason behind this or Am i missing anything?

Aju John
  • 2,214
  • 1
  • 10
  • 27
  • 6
    `non-numeric` properties don't add to the length of the array – gurvinder372 Nov 27 '17 at 06:11
  • Based on your result, it works properly. Array always starts from zero, not from one. In second scenario, you are adding by key, the array pattern must match when you append. – slon Nov 27 '17 at 06:15
  • 1
    Your answer might be here: https://stackoverflow.com/questions/9526860/why-does-a-string-index-in-a-javascript-array-not-increase-the-length-size – Mamun Nov 27 '17 at 06:27

5 Answers5

5

As per spec

The value of the length property is numerically greater than the name of every own property whose name is an array index; whenever an own property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant.

Further

Specifically, whenever an own property is added whose name is an array index, the value of the length property is changed.

So, length property is adjusted when the property whose name is an array index is created, only then length property changes.

'abc' being a non-numeric (and hence not an index of an array) when added to the array, doesn't reflect any change in length property.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
2

By writing

var x = [ ];

you are defining a standard array where indexes can only be integers.

x['abc'] = "hello";

When you do x["abc"] since abc (which is what you use as index) is not an integer you are basically defining a property to the arr object (everything is object in javascript) but you are not adding an element to the standard array. and hence you array remains as it was.

var x = [];
x["abc"] = "hello";
console.log(x);

and that is the reason the size of the array is 0 in the first case and 2 in the second

When you wish to add property to x with string keys, you need to define you variable as an object.

var x = {}
x['abc'] = "hello";

and then you can get the size of you Object(which is indeed the number of keys in your object) by

Object.keys(x).length
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0

you cannot access the array without the index. If you want to insert particular data in an array you can give it like,

 var x = [];
    x[0] = "hello";
    alert(x.length) //will return 1
Dhivya
  • 1
  • 2
0

What you have done is that you have converted your array into an associative array, where your indices are the reference to the value assigned. This is similar to the objects concept in Javascript.

The length is 0 because a real array starts from index 0. So if you check for your array

console.log(x[0]) //undefined

hence, the length 0.

Roy
  • 471
  • 5
  • 15
0

You cannot append a user defined key that isn't a number in JavaScript, as you can do in Python or Php etc. In JavaScript the key's of the Array shall only be numeric.

Thus doing:

var x = [];
x['abc'] = "hello";
console.log(x.length);

returns the length of x as 0. As nothing has been pushed into the array.If you do console.log(x), you'll find out that the array has no values or is empty.

Whereas doing this:

var x = [1, 2];
x['abc'] = "hello";
console.log(x.length); //returns 2

returns the length as 0 because the array consists of number 1 at the index 0 and the number 2 at the index/position 1 of that array. Just those values are there in the array, if you want to push "hello" push it like, x.push("hello") which will push it at the index 2 of the array.

If you still want to use alphabetic index use Objects instead.

Karan Dhir
  • 731
  • 1
  • 6
  • 24