Writing table of number in array in JavaScript

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m.

Therefore, let's write the code for this function ?

Example

The code for this will be ?

const num1 = 4;
const num2 = 6;
const multiples = (num1, num2) => {
    const res = [];
    for(let i = num1; i <= num1 * num2; i += num1){
        res.push(i);
    };
    return res;
};
console.log(multiples(num1, num2));

Output

The output in the console will be ?

[ 4, 8, 12, 16, 20, 24 ]

Alternative Approach Using Array.from()

We can also use Array.from() with a mapping function to generate the multiplication table:

const generateTable = (base, count) => {
    return Array.from({length: count}, (_, index) => base * (index + 1));
};

console.log(generateTable(7, 5));
console.log(generateTable(3, 8));
[ 7, 14, 21, 28, 35 ]
[ 3, 6, 9, 12, 15, 18, 21, 24 ]

Using a Simple For Loop

Here's another straightforward approach using a basic for loop:

function createMultiplicationTable(number, count) {
    const table = [];
    for (let i = 1; i <= count; i++) {
        table.push(number * i);
    }
    return table;
}

console.log(createMultiplicationTable(5, 4));
console.log(createMultiplicationTable(9, 3));
[ 5, 10, 15, 20 ]
[ 9, 18, 27 ]

Conclusion

All three methods effectively generate multiplication tables as arrays. The for loop approach is most readable, while Array.from() offers a more functional programming style.

Updated on: 2026-03-15T23:19:00+05:30

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements