0

That's my first own question on StackOwerflow.

My problem in JavaScript's arrays is:

var arr=[];
arr[0] = 'a';
arr[1] = 'b';
arr['2']='c';
arr['x']='d';
arr.y='e';
console.log(arr);
[ 'a', 'b', 'c', x: 'd', y: 'e' ] -- this is what i get 

And i do not understand what is that actually:

arr['2']='c';
arr['x']='d';
arr.y='e';

I did not expected that behaviour because i've not seen that examples and theory before

  • maybe you can read this https://stackoverflow.com/questions/67030137/why-does-an-array-allow-a-string-as-an-index-in-javascript – Damzaky Jan 24 '23 at 11:48
  • @Damzaky Thank you!!! Didn't found it before :) – Oksana Smirnova Jan 24 '23 at 11:50
  • The OP created exactly 3 valid array-**items**. The indices are `0`, `1` and `'2'` with the first two being integer number-values and the latter being a string which represents another valid array index. Thus the OP's array features a `length` value of `3`. All other assignments just add additional **properties**, like `x` and `y` to the OP's array. Thus they are not recognized as iterable array-items. – Peter Seliger Jan 24 '23 at 11:55
  • @OksanaSmirnova ... _" Didn't found it before :)"_ ... really ?.. [MDN documentation - array indices](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#array_indices) – Peter Seliger Jan 24 '23 at 11:59

0 Answers0