Reputation: 11
This is my JavaScript array code for negative index number. In output why doesn't consider negative index number in the count of the element? It shows only count (3) in output.
Code
let abc = ['gnagar', 'ahmedabad', 25];
console.log(abc, typeof(abc));
console.log(abc[-1]);
abc[-1] = 'abc';
console.log(abc, typeof(abc));
console.log(abc[-1]);
Upvotes: 0
Views: 191
Reputation: 7739
This is because array
is type of object as you can see there typeof(abc)
is object.
You can assign values in objects using []
.
Negative indexes are not actual index so it does not impact the array length.
Upvotes: 0
Reputation: 534
-1
is not a valid index for array.
The assignment abc[-1] = 'abc';
means set attribute "-1" to the abc object.
Upvotes: 1