JavaScript LCM Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is LCM?

The least common multiple (LCM) of two integers is the smallest positive number that is divisible by both.

Remember
lcm(a, b) = smallest positive m such that
            m % a == 0 and m % b == 0

Identity (nonnegative a, b):
  gcd(a, b) × lcm(a, b) = a × b
  ⇒  lcm(a, b) = |a × b| / gcd(a, b)

Example: a = 12, b = 18
  Multiples of 12: 12, 24, 36, 48, …
  Multiples of 18: 18, 36, 54, …
  First shared = 36
  Check: gcd(12, 18) = 6 → 12 × 18 / 6 = 36
abgcdlcm
1218636
75135
015150

How to Find LCM in JavaScript

Four clear steps — prefer the GCD formula in interviews and real code.

  1. Take absolute values with Math.abs so negatives do not change the LCM.
  2. If either value is 0, return 0.
  3. Compute gcd(a, b) with Euclid’s algorithm.
  4. Return |a / gcd × b| (divide before multiply to reduce product size).
MethodIdeaTimeBest for
GCD formula|a / gcd × b|O(log min)Interviews, production
Multiple scanIncrease by max(a, b) until both divideO(lcm / max)Teaching the definition

Pseudocode

Pseudocode
function Gcd(a, b):
    a = abs(a), b = abs(b)
    while b != 0:
        t = b
        b = a mod b
        a = t
    return a

function Lcm(a, b):
    a = abs(a), b = abs(b)
    if a == 0 or b == 0:
        return 0
    g = Gcd(a, b)
    return (a / g) * b

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
MeaningSmallest positive multiple of both
Formulalcm = |a × b| / gcd(a, b)
Safe orderDivide by gcd first, then multiply
Zerolcm(0, n) = 0
Three numberslcm(lcm(a, b), c)
Classic checklcm(12, 18) === 36

Live Preview

Enter two integers to see their GCD and LCM.

Whole numbers (absolute values used). Tap a chip or type a pair.

Result
a = 12, b = 18
gcd(12, 18) = 6
lcm = |12 × 18| / 6 = 36

JavaScript Programs

Start with the GCD formula — fast and interview-ready. Use the multiple scan when you want to show the definition directly.

Example 1 — GCD Formula

Compute Euclid’s GCD, then return |a / gcd × b|.

JavaScript
function gcd(a, b) {
  a = Math.abs(a);
  b = Math.abs(b);
  while (b !== 0) {
    const t = b;
    b = a % b;
    a = t;
  }
  return a;
}

function lcm(a, b) {
  a = Math.abs(a);
  b = Math.abs(b);
  if (a === 0 || b === 0) {
    return 0;
  }
  return (a / gcd(a, b)) * b;
}

console.log("LCM = " + lcm(12, 18));
Try It Yourself

How It Works

  1. Take absolute values so sign does not matter.
  2. If either side is 0, return 0.
  3. gcd repeats a % b until the remainder is 0.
  4. For 12 and 18, gcd is 6.
  5. (12 / 6) * 18 = 36 — divide first to keep the product smaller.

Example 2 — Multiple Scan

Start from the larger number and step by that size until both divide evenly.

JavaScript
function lcm(a, b) {
  a = Math.abs(a);
  b = Math.abs(b);
  if (a === 0 || b === 0) {
    return 0;
  }

  const step = Math.max(a, b);
  let candidate = step;
  while (true) {
    if (candidate % a === 0 && candidate % b === 0) {
      return candidate;
    }
    candidate += step;
  }
}

console.log(lcm(12, 18)); // 36
console.log(lcm(7, 5));   // 35
Try It Yourself

How It Works

  1. Handle zeros and absolute values like Example 1.
  2. Set step = Math.max(a, b) — the LCM cannot be smaller than that.
  3. Test candidate; if both divide it, you found the LCM.
  4. Otherwise add step and try again.
  5. Clear for teaching, but slower than the GCD formula for large values.

Edge Cases & Pitfalls

Check these before calling the solution done.

Zero

lcm(0, n) = 0

Return early so you never mishandle a zero input.

Overflow

Divide first

Use (a / gcd) * b. For very large values, consider BigInt.

Negatives

Use Math.abs

LCM is usually reported as a nonnegative value.

Coprime

gcd = 1

Then lcm(a, b) = |a × b| — still correct.

Time and Space Complexity

ProgramTimeExtra space
GCD formulaO(log min(a, b))O(1)
Multiple scanO(lcm / max(a, b))O(1)

Euclid’s GCD dominates the formula method. The scan walks multiples until it hits the LCM.

Key Takeaways

  • Definition: smallest positive number divisible by both inputs.
  • Formula: lcm = |a × b| / gcd(a, b).
  • Safety: divide by gcd before multiplying; use BigInt for huge values.
  • Test set: (12, 18) → 36; (7, 5) → 35; (0, 15) → 0.

One line: find gcd, then compute |a / gcd × b|.

Frequently Asked Questions

The least common multiple of a and b is the smallest positive integer divisible by both (for positive a and b).
For nonnegative integers, gcd(a, b) × lcm(a, b) = a × b, so lcm(a, b) = |a × b| / gcd(a, b).
By common programming convention, lcm(0, n) = 0.
Computing |a / gcd × b| keeps intermediate values smaller than a × b first, which helps avoid overflow with large numbers.
Fold pairwise: lcm(lcm(a, b), c).

Next: Leap Year

Check whether a year is a leap year in JavaScript.

Leap year tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful