Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Multiplication Table in JavaScript

Print a multiplication table in JavaScript using for and while loops and a reusable function. Covers off-by-one bugs, with explanation and output.

BeginnerJavaScript Loop ProgramsExample 1 of 6
multiplication-table.js
1// Method 1: Using for loop
2let number = 5;
3console.log("Multiplication table of", number + ":");
4
5for (let i = 1; i <= 10; i++) {
6 console.log(number + " × " + i + " = " + (number * i));
7}
8
9// Method 2: Using template literals
10let num = 7;
11console.log("\nMultiplication table of " + num + ":");
12
13for (let i = 1; i <= 10; i++) {
14 console.log(`${num} × ${i} = ${num * i}`);
15}
16
17// Method 3: Using while loop
18let n = 9;
19let counter = 1;
20console.log("\nMultiplication table of " + n + ":");
21
22while (counter <= 10) {
23 console.log(n + " × " + counter + " = " + (n * counter));
24 counter++;
25}
26
27// Method 4: Function to generate table
28function 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}
34
35multiplicationTable(6);

Sample 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

In this one

  • The three parts of a for loop
  • How to avoid off-by-one errors
  • How a while loop differs from a for loop
  • How default parameters work

How it works

  1. 1

    number = 5. The for loop runs with i from 1 to 10.

  2. 2

    Each round prints 5 × i = result.

  3. 3

    The table of 7 repeats this with a template literal.

  4. 4

    The while loop prints the table of 9 and increases counter by hand.

  5. 5

    multiplicationTable(6) prints the table of 6.

Edge cases

Using < instead of <=

i < 10 prints only up to 9.

Use i <= 10 when the last value must be included.

Forgetting counter++

The while loop runs forever.

The condition never becomes false. Always change the counter inside a while loop.

Number from prompt

"5" * i still works, but "5" + " × " is fine too.

* converts strings, so this case works. Convert with Number() anyway for safety.

What's going on

This program prints the multiplication table of a number from 1 to 10. It is a classic first loop exercise. A loop repeats a block of code so you do not write the same line ten times.

Method 1 uses a for loop: for (let i = 1; i <= 10; i++). It has three parts. let i = 1 starts the counter. i <= 10 is the condition checked before each round. i++ adds 1 after each round.

Look closely at <=. If you write i < 10, the loop stops at 9 and you get only nine rows. That missing row is called an off-by-one error. It is one of the most common loop bugs. If you want 10 rows, the condition has to include 10.

Method 2 does the same table for 7 but prints with a template literal. ${num} × ${i} = ${num * i} keeps each line readable without lots of + signs.

Method 3 uses a while loop for 9. A while loop only has a condition. You create the counter before it and increase it inside it. If you forget counter++, the loop never ends.

Method 4 wraps the loop in a function with a default parameter, limit = 10. You can call multiplicationTable(6) for a normal table or multiplicationTable(6, 20) for a longer one.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

How many times does for (let i = 1; i < 5; i++) run?

Pick an answer to check it.

Question 2

What happens if you forget counter++ in a while loop?

Pick an answer to check it.

Questions people ask

Why does i start at 1?

Tables start at 1 × n. Starting at 0 would add a 0 row.

Can I print the table up to 20?

Yes. Call multiplicationTable(n, 20).

What is the × character?

It is a multiplication sign used for display only. The code still uses * for math.