Suggest an editImprove this articleRefine the answer for “Accessing a non-existent array index”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Accessing a non-existent array index returns `undefined` and does not throw.** An array in JavaScript is an object in which indexes are ordinary string keys, and reading a missing property of an object always yields `undefined` rather than an exception, unlike in some other languages. A `TypeError` only happens when the array itself does not exist, that is, when the variable is `null` or `undefined`. To tell a missing element from a stored `undefined`, use the `in` operator or `Object.hasOwn()`. ```javascript const arr = ['apple', 'banana', 'cherry']; console.log(arr[0]); // 'apple' console.log(arr[10]); // undefined, no error console.log(10 in arr); // false ``` **Key point:** a non-existent index gives `undefined` without an error; a `TypeError` appears only when the array itself is `null` or `undefined`.Shown above the full answer for quick recall.Answer (EN)Image**If you access a non-existent array index, JavaScript returns `undefined` and no error is thrown.** This is not a bug but a consequence of arrays being objects, and reading a missing property of an object in JavaScript is always safe. ## Theory ### TL;DR - Reading a non-existent index yields `undefined`, **no exception is thrown**. - The reason: an array is an object, indexes are string keys, and a missing property returns `undefined`. - `undefined` here means "the value is absent", not "an error occurred". - A `TypeError` appears only when **the array itself** does not exist: `null` or `undefined`. - You can check for presence with `index in arr` or `Object.hasOwn(arr, index)`. - An empty slot (a hole) also yields `undefined`, but `in` returns `false` for it. ### Quick example ```javascript const arr = ['apple', 'banana', 'cherry']; console.log(arr[0]); // 'apple' console.log(arr[3]); // undefined, there is no such element console.log(arr[10]); // undefined, and again no error ``` None of these reads stops the execution of the code. ### Why it works this way In JavaScript an array is an **object** in which indexes are **string keys**: ```javascript // roughly equivalent const arr = { '0': 'apple', '1': 'banana', '2': 'cherry', length: 3 }; ``` When you access a **non-existent property**, JavaScript returns `undefined` instead of throwing, unlike Python with its `IndexError` or Java with `ArrayIndexOutOfBoundsException`. The same holds for negative and fractional indexes: they simply become ordinary string keys that the object does not have. ```javascript const arr = [1, 2, 3]; console.log(arr[-1]); // undefined, this is the key '-1', not "the last element" console.log(arr[1.5]); // undefined console.log(arr.at(-1)); // 3, this is how you take the last element ``` ### When you do get an error `undefined` means **"the value is absent"**, not "an error". But if you try to access an index of an array that does not exist at all, that is, the array itself is `undefined` or `null`, then you do get an exception: ```javascript const arr = undefined; console.log(arr[0]); // TypeError: Cannot read properties of undefined ``` Optional chaining keeps such a read safe: ```javascript const arr = undefined; console.log(arr?.[0]); // undefined, no error console.log(arr?.[0] ?? 0); // 0, a default value ``` ### How to check that an element exists ```javascript const arr = [1, 2, 3]; console.log(2 in arr); // true, the element at index 2 exists console.log(5 in arr); // false, there is no such element ``` A more modern and safer alternative is `Object.hasOwn()`: ```javascript console.log(Object.hasOwn(arr, 2)); // true console.log(Object.hasOwn(arr, 5)); // false ``` The check `arr[i] !== undefined` is not suitable here: it cannot tell a missing element from a stored `undefined` value. ```javascript const values = [1, undefined, 3]; console.log(values[1] !== undefined); // false, even though the element exists console.log(1 in values); // true ``` ### An example with holes (empty slots) ```javascript const arr = [1, , 3]; // a skipped element console.log(arr[1]); // undefined console.log(1 in arr); // false, the cell really is not there ``` So `arr[1]` gives `undefined`, yet the slot **does not exist**: it is an empty cell, not a stored `undefined` value. ### A short table | Situation | Result | Error? | | --- | --- | --- | | The index exists | returns the value | No | | There is no such index | `undefined` | No | | The array does not exist (`null` or `undefined`) | `TypeError` | Yes | | An empty slot (a hole) | `undefined`, but `index in arr` gives `false` | No | > **Summary:** accessing a non-existent array index returns `undefined` but **does not raise an error**. That behaviour is part of the flexibility of JavaScript. ### Common mistakes - **Expecting an exception, as in other languages.** Going out of bounds is silent in JavaScript, so you will see the failure much later, already as an `undefined`. - **Confusing "no element" with "the value is `undefined`".** A comparison against `undefined` gives no answer; use `in` or `Object.hasOwn()`. - **Using `arr[-1]` as the last element.** That is just the key `'-1'`; for the last element use `arr.at(-1)` or `arr[arr.length - 1]`. - **Forgetting to check the array itself.** If the data came from the network, the variable may be `undefined`, and then the read throws a `TypeError`; guard it with `arr?.[i]`. - **Chaining reads without a guard.** `arr[10].name` throws a `TypeError`, because `undefined` has no properties; write `arr[10]?.name`. - **Writing to a large index.** `arr[100] = 1` does not throw, it stretches `length` to `101` and creates holes.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.