To display JavaScript array elements, you can read a specific index, loop through all values, join them into a string, log them to the console, or create DOM elements for display on a web page.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
for (let i = 0; i < scripts.length; i++) {
console.log(scripts[i]);
}
The loop starts at index 0 and continues while i < scripts.length, so it visits every existing index from the first to the last.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
console.log(scripts[2]); // JavaScript
If the index does not exist, the result is undefined.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
console.log(scripts[0]); // PHP
console.log(scripts[scripts.length - 1]); // HTML
Do not use scripts[scripts.length] for the last item because that index is one position beyond the array.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
for (let i = 0; i < scripts.length; i++) {
console.log(`${i}: ${scripts[i]}`);
}
0: PHP
1: ASP
2: JavaScript
3: HTML
A classic for loop is especially useful when your logic needs the numeric index.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
for (const script of scripts) {
console.log(script);
}
for...of is usually simpler when you only need each value and not its index.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
scripts.forEach((script, index) => {
console.log(index, script);
});
forEach() calls your callback once for each visited element. Unlike a normal loop, you cannot stop it early with break.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
for (const [index, script] of scripts.entries()) {
console.log(index, script);
}
This is a readable alternative when you want both index and value but prefer for...of syntax.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
console.log(scripts.join(" : ")); // PHP : ASP : JavaScript : HTML
console.log(scripts.toString()); // PHP,ASP,JavaScript,HTML
Use join() when you need a custom separator. toString() uses commas.
For page output, create DOM elements and set textContent. This avoids treating array values as HTML.
<ul id="languageList"></ul>
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
const list = document.getElementById("languageList");
for (const script of scripts) {
const item = document.createElement("li");
item.textContent = script;
list.appendChild(item);
}
This is preferable to using document.write(), which can replace an already-loaded document and is not appropriate for normal modern page rendering.
The original page demonstrated for...in to show array keys. It is valid JavaScript, but for...in enumerates property names and can include additional enumerable properties. For array values, prefer an index loop, for...of, entries(), or an array method.
const scripts = ["PHP", "ASP", "JavaScript"];
for (const key in scripts) {
console.log(key, scripts[key]);
}
for...in is more naturally suited to enumerable object property names than normal array traversal.const values = ["A", , undefined, "D"];
for (let i = 0; i < values.length; i++) {
console.log(i, values[i], i in values);
}
An empty slot and an explicitly stored undefined can both read as undefined, but i in values distinguishes whether the property actually exists.
When an array changes, rerun a rendering function that clears the old DOM output and builds it from the current array state.
function renderList(values, list) {
list.replaceChildren();
for (const value of values) {
const item = document.createElement("li");
item.textContent = value;
list.appendChild(item);
}
}
This separates data management from display logic and avoids concatenating HTML strings from untrusted values.
i <= array.length and reading one index too far.array[array.length] as the last element.for...in without understanding that it enumerates property names.innerHTML when plain text is intended.forEach() to support break.Create Arrays Array length Display with join() Add or Remove by Position Display Two-Dimensional Arrays Array Reference
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.