Multiplication Table in JavaScript
for (let i = 1; i <= 10; i++). The extra = is the off-by-one.
BeginnerLoop ProgramsExample 1 of 6
multiplication-table.js
1// Method 1: Using for loop2let number = 5;3console.log("Multiplication table of", number + ":");45for (let i = 1; i <= 10; i++) {6 console.log(number + " × " + i + " = " + (number * i));7}89// Method 2: Using template literals10let num = 7;11console.log("\nMultiplication table of " + num + ":");1213for (let i = 1; i <= 10; i++) {14 console.log(`${num} × ${i} = ${num * i}`);15}1617// Method 3: Using while loop18let n = 9;19let counter = 1;20console.log("\nMultiplication table of " + n + ":");2122while (counter <= 10) {23 console.log(n + " × " + counter + " = " + (n * counter));24 counter++;25}2627// Method 4: Function to generate table28function multiplicationTable(number, limit = 10) {29 console.log(`Multiplication table of ${number}:`);30 for (let i = 1; i <= limit; i++) {31 console.log(`${number} × ${i} = ${number * i}`);32 }33}3435multiplicationTable(6);
Output
Multiplication table of 5: 5 × 1 = 5 5 × 2 = 10 5 × 3 = 15 5 × 4 = 20 5 × 5 = 25 5 × 6 = 30 5 × 7 = 35 5 × 8 = 40 5 × 9 = 45 5 × 10 = 50 Multiplication table of 7: 7 × 1 = 7 7 × 2 = 14 7 × 3 = 21 7 × 4 = 28 7 × 5 = 35 7 × 6 = 42 7 × 7 = 49 7 × 8 = 56 7 × 9 = 63 7 × 10 = 70 Multiplication table of 9: 9 × 1 = 9 9 × 2 = 18 9 × 3 = 27 9 × 4 = 36 9 × 5 = 45 9 × 6 = 54 9 × 7 = 63 9 × 8 = 72 9 × 9 = 81 9 × 10 = 90 Multiplication table of 6: 6 × 1 = 6 6 × 2 = 12 6 × 3 = 18 6 × 4 = 24 6 × 5 = 30 6 × 6 = 36 6 × 7 = 42 6 × 8 = 48 6 × 9 = 54 6 × 10 = 60
What's going on
i <= 10 not i < 10. Want 10 rows? The loop has to include 10.
Template strings keep 5 x 3 = 15 readable.