JavaScript Tips
How to Calculate the Least Common Multiple in JavaScript
An introduction to the LCM and how to implement it in JavaScript

The problem today is about calculating the least common multiple of a set of numbers. In mathematics, the least common multiple of two positive integers a and b, denoted by LCM(a,b), is the smallest natural number that is divisible by both a and b. But let's start with the definition of the problem:
The Problem: Least Common Multiple
link to the kata
Write a function that calculates the least common multiple of its arguments; each argument is assumed to be a non-negative integer. In the case that there are no arguments (or the provided array in compiled languages is empty), return 1. If any argument is 0, return 0.
The Solution

To solve the problem, I can follow the instructions on Wikipedia:

It is essentially a matter of using the greatest common divisor. But how do you calculate the greatest common divisor? I talked about it a few days ago in another post:
const gcd = (x, y) => (y === 0 ? x : gcd(y, x % y));Therefore, the problem of calculating the least common multiple in JavaScript can be solved in just two lines:
const gcd = (x, y) => (y === 0 ? x : gcd(y, x % y));
const lcm = (...n) => n.reduce((x, y) => (x * y) / gcd(x, y));Thanks for reading! Stay tuned for more.
Don’t miss my next article — sign up for my Medium email list
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
Looking to scale your software startup? Check out Circuit.






