Skip to main content

Generate a 9×9 Multiplication Table with JavaScript

· One min read
Apache Wangye
Software developer and technical writer

A multiplication table is a simple exercise for understanding nested loops.

Console output:

for (let row = 1; row <= 9; row++) {
const cells = [];
for (let column = 1; column <= row; column++) {
cells.push(`${column} × ${row} = ${column * row}`);
}
console.log(cells.join("\t"));
}

DOM rendering:

const table = document.createElement("table");

for (let row = 1; row <= 9; row++) {
const tr = document.createElement("tr");
for (let column = 1; column <= row; column++) {
const td = document.createElement("td");
td.textContent = `${column} × ${row} = ${column * row}`;
tr.appendChild(td);
}
table.appendChild(tr);
}

document.body.appendChild(table);

Using textContent avoids HTML injection and makes the generated structure easy to style with CSS.

Page views: --

Total views -- · Visitors --