Похожие чаты

I read something strange about JavaScript arrays today. The length

method in JS acts differently in js compared to other languages.
I mean even when we have like 4 elements, and greatest index let's say 10... The length gives value as 11.
So is it always wise to use length method of arrays in JS.
Doesn't it make it unreliable?

1 ответов

5 просмотров

const arr = []; console.assert(arr.length === 0); arr.push(1, 2, 3); console.assert(arr.length === 3); arr.pop(); console.assert(arr.length === 2); arr[arr.length] = 10; console.assert(arr.length === 3); delete arr[arr.length - 1]; console.assert(arr[2] === undefined); console.assert(arr.length === 3); arr[100] = 42; console.assert(arr.length === 101); arr.length = 2; console.assert(arr[100] === undefined); console.assert(arr.length === 2);

Похожие вопросы

Обсуждают сегодня

Карта сайта