To retrieve the first element of a JavaScript array without mutating it, use arr.at(0) or bracket indexing arr[0]. Both read index 0 directly without modifying the original array and return undefined when the array is empty.
1. Reading the First Element with arr.at(0) and arr[0]
Use arr.at(0) or arr[0] to inspect the first item cleanly while leaving the source array intact:
// Retrieve the first element without mutating the array
function getFirstItem(items) {
if (!Array.isArray(items) || items.length === 0) {
return undefined;
}
return items.at(0);
}
const queue = ["task-101", "task-102", "task-103"];
const firstWithAt = queue.at(0);
const firstWithBracket = queue[0];
console.log("First via .at(0):", firstWithAt); // "task-101"
console.log("First via [0]:", firstWithBracket); // "task-101"
console.log("Original array unchanged:", queue); // ["task-101", "task-102", "task-103"]
arr[0]: Fast, universal index access that returnsundefinedifarris empty ([]).arr.at(0): Modern ES2022 method that reads index0cleanly and supports negative indices likearr.at(-1)when you also need the last element.- Non-mutating: Neither
arr.at(0)norarr[0]changesarr.lengthor alters any elements insidearr.
2. Destructuring with a Default Fallback Value
Array destructuring (const [first = defaultValue] = arr;) also avoids mutating the array and lets you supply a fallback when the array is empty or its first slot is undefined:
// Extract the first element with a default fallback when empty or undefined
function getFirstOrDefault(items, fallback = null) {
if (!Array.isArray(items)) {
return fallback;
}
const [first = fallback] = items;
return first;
}
console.log("Non-empty:", getFirstOrDefault(["alpha", "beta"], "none")); // "alpha"
console.log("Empty array:", getFirstOrDefault([], "none")); // "none"
- Default value triggers on
undefined:[first = "none"] = [undefined]evaluatesfirstto"none". - Does not mutate
items: Destructuring reads elements without altering the array.
3. Common Traps to Avoid
Retrieving the first element of an array can cause side effects or subtle bugs if mutating methods are used by mistake:
- Using
arr.shift()just to read the first element:arr.shift()mutates the array by removing the first element and shifting all remaining indices down—only use.shift()when you intentionally want to dequeue/remove the first item. - Using
arr.splice(0, 1)[0]for read-only access:.splice()also mutates the original array in place. - Not guarding empty arrays when chaining property access: If
itemsis empty ([]),items[0]isundefined, soitems[0].idthrows aTypeErrorunless you guard or use optional chainingitems[0]?.id.
TL;DR: Use arr.at(0) or arr[0] (or const [first = fallback] = arr for defaults) to read the first element without mutating the array—never use .shift() or .splice() unless you intend to remove the item.
Are you reading the first element of an array of objects, providing fallback defaults for empty arrays, or comparing read-only access against removing items with .shift()?
