Declare, read, and update arrays in JavaScript
Declare a JavaScript array with square brackets. In const items = ['A', 'B'], read the first entry with items[0] and replace it with an assignment such as items[0] = 'C'. Read the length with items.length and append with items.push(value).
Declare an array and read elements by index
Separate values with commas in an array literal. [] makes an empty array. Run the code in the console. Declaring words with const prevents binding that name to a different array, while its elements can still change.
const words = ['Lorem', 'Ipsum', 'Dolor'];
console.log(words[0]); // 'Lorem'
console.log(words[2]); // 'Dolor'
console.log(words.length); // 3
console.log(words[3]); // undefined
words[1] = "New value";
console.log(words); // ['Lorem', "New value", 'Dolor']
The last index of a three-element array is 2. Here words[3] gives undefined. Check for an empty array before using its first element.
Treat a single number carefully in the Array constructor
new Array('A', 'B') stores two elements. With a single number, new Array(3) creates an array with length 3 and no elements. To store the number 3, use [3] or Array.of(3). new Array(3.5) throws a RangeError because the length is invalid. See ECMAScript: Array().
console.log([3]); // [3]
console.log(Array.of(3)); // [3]
const slots = new Array(3);
console.log(slots.length); // 3
console.log(Object.hasOwn(slots, 0)); // false
console.log(slots[0]); // undefined
slots[0] = undefined;
console.log(slots[0]); // undefined
console.log(Object.hasOwn(slots, 0)); // true
Reading an empty slot or an element explicitly set to undefined produces the same value in the example. Object.hasOwn(slots, 0) is false before assignment and true after assigning undefined. The assignment creates the array’s own property at that index. See ECMAScript: Object.hasOwn().
Replace an element or add one at the end
Assigning to an existing index replaces its value and keeps the same length. push() appends a value and returns the new length. Assigning beyond the current length leaves holes, as in the code below. Use push() to keep the list contiguous.
const items = [];
items[0] = 'A';
items[1] = 'B';
console.log(items); // ['A', 'B']
items[4] = 'E';
console.log(items.length); // 5
console.log(2 in items); // false
In this assignment example, the length is 5, but only indexes 0, 1, and 4 contain values. Interpret length as the element count only for a list with no holes. See ECMAScript: Array length for how the length changes.
Complete example: Append, replace, and reset
Save the HTML in UTF-8 as array-declare.html and open it in a browser. Append calls push(). Replace first assigns to items[0]. Both update the same array.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Append and replace array elements</title>
</head>
<body>
<label for="item">Value to use</label>
<input id="item" type="text">
<button id="add" type="button">Append</button>
<button id="replace" type="button">Replace first</button>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite"></pre>
<p id="status" role="status"></p>
<script>
const initialItems = ['Lorem', 'Ipsum', 'Dolor'];
const items = [...initialItems];
const input = document.querySelector('#item');
const output = document.querySelector('#output');
const status = document.querySelector('#status');
function show() {
output.textContent = `Array: ${JSON.stringify(items)}\n` +
`First element: ${items[0]}\n` +
`length: ${items.length}`;
}
function update(replaceFirst) {
const value = input.value.trim();
if (value === '') {
status.textContent = "Enter a value.";
return;
}
if (replaceFirst) {
items[0] = value;
} else {
items.push(value);
}
input.value = '';
status.textContent = '';
show();
}
document.querySelector('#add').addEventListener('click', () => update(false));
document.querySelector('#replace').addEventListener('click', () => update(true));
document.querySelector('#reset').addEventListener('click', () => {
items.length = 0;
items.push(...initialItems);
input.value = '';
status.textContent = '';
show();
});
show();
</script>
</body>
</html>
The array starts with three values. Append Alpha to make the length 4. Then enter Beta and click Replace first. The length stays 4. Whitespace alone shows a message and leaves the array and output in place. Reset empties the same array and restores the three starting values.




