Negative Indices and Non-Integer Keys
You'll learn: Why using negative or non-integer indices on arrays doesn't create array elements but instead adds object properties.
Arrays Are Objects in Disguise
Remember that JavaScript arrays are actually special objects. They have a length property and treat non-negative integer indices (0, 1, 2, ...) as array elements. But what happens when you use a negative number or a non-integer as a "key"?
Here's the surprise: negative indices and non-integer keys become regular object properties, not array elements.
const arr = ['a', 'b', 'c'];
arr[-1] = 'negative'; // Adds a property named "-1"
arr[1.5] = 'decimal'; // Adds a property named "1.5"
arr['foo'] = 'bar'; // Adds a property named "foo"
console.log(arr.length); // Still 3!
console.log(arr); // ['a', 'b', 'c']
The array's length remains 3 because none of these keys are valid array indices. They don't affect the array's element count.
Why This Matters
This behavior can catch you off guard if you're expecting Python-like negative indexing (where arr[-1] accesses the last element). In JavaScript, you're actually creating an object property that lives alongside the array elements but doesn't participate in array operations.
const nums = [10, 20, 30];
nums[-1] = 'oops';
console.log(nums[-1]); // 'oops' (property access)
console.log(nums[2]); // 30 (actual last element)
Key Takeaway: Array indices must be non-negative integers from 0 to 2³²-1. Any other key—negative numbers, decimals, or strings—creates an object property that doesn't count toward length and won't appear in array-specific operations.