Open In App

JavaScript Program to Print Multiplication Table of a Number

Last Updated : 24 May, 2025
Suggest changes
Share
Like Article
Like
Report

We are given a number n as input, we need to print its table. Below are the different approaches to print multiplication table in JavaScript

JavaScript Program to print multiplication table of a number

1. Using a for Loop

This is the most common way to print the multiplication table. A for loop repeats the multiplication from 1 to 10 and displays the result for each number.

JavaScript
let n = 5

for (let i = 1; i <= 10; i++) {
    console.log(`${n} x ${i} = ${n * i}`);
}

Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

2. Using a while Loop

The while loop is another way to print the multiplication table. It repeatedly executes a block of code as long as a specified condition is true. In this case, the loop will run as long as i is less than or equal to 10, and each iteration will print the multiplication of the given number and i.

JavaScript
let n=5
let i = 1;
while (i <= 10) {
    console.log(`${n} x ${i} = ${n * i}`);
    i++;
}

Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

3. Using Array.forEach() Method

The Array.forEach() method is a functional programming approach that allows you to iterate over each element in an array and apply a function to each of them.

JavaScript
let n=5;

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach(i => {
    console.log(`${n} x ${i} = ${n * i}`);
});

Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

4. Using Recursion in JavaScript

Recursion in JavaScript refers to a function calling itself to solve a problem. This approach is an alternative way to print the multiplication table without using loops.

JavaScript
function print_table(n, i = 1) {
    if (i == 11) // Base case
        return;
    console.log(n + " * " + i + " = " + n * i);
    i++;  // Increment i
    print_table(n, i);
}

// Driver Code
let n = 5;
print_table(n);

Output
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

5. Using Array.from() method

The Array.from() method in JavaScript is a powerful and flexible way to create arrays from array-like or iterable objects, such as strings, NodeLists, or even objects with a length property.

JavaScript
function printTable(num) {
    Array.from({ length: 10 }, (_, i) => console.log(`${num} * ${i + 1} = ${
    num * (i + 1)}`));
}


printTable(12);

Output
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120

Similar Reads