5 ms·
I see people bringing up arrays, and an array index is represented by a number, you can do math on it, but it's not a regular number for counting a sequence of
by calibas 2y ago
I see people bringing up arrays, and an array index is represented by a number, you can do math on it, but it's not a regular number for counting a sequence of items. It's a unique reference to a location in the memory, and it's dangerous to treat an array index like it's just any old number.
Behold, the really stupid things you can do in Javascript:
let myArr = [];
let index = 0;
myArr[--index] = 5;
console.log(myArr.length); // 0
console.log(myArr[index]); // 5
- IshKebab 2y agoAn index is a specific kind of number, but so is a count. Indexing should clearly start from 0. It leads to far more elegant code and lower risk of off-by-one mistakes.