Find GCD in JavaScript (Euclidean algorithm, tested output)

Tech reviewed: Deepak Prasad
Find GCD in JavaScript (Euclidean algorithm, tested output)

The greatest common divisor (GCD) of integers is the largest positive integer that divides each of them with remainder zero. (It is not a “denominator”—that word belongs to fractions.) For gcd javascript, javascript gcd, or greatest common divisor javascript, the standard approach is Euclid’s algorithm using the modulo form gcd(a, b) = gcd(b, a % b) until the second argument becomes zero.

Tested on: all examples in this tutorial were run with Node.js v20.18.2, and the output shown below each snippet is its exact console output.


Quick reference

Use this table for javascript gcd function choices.

Need Approach
Two integers, no deps Euclidean loop or recursion with Math.abs
Array of integers reduce with pairwise gcd
Huge integers BigInt + same algorithm
Many args, convenience mathjs gcd

1. Recursive Euclidean javascript gcd

javascript
function gcd(a, b) {
  if (b === 0) {
    return a;
  }
  return gcd(b, a % b);
}

console.log(gcd(12, 18));
text
6

2. Iterative form (often preferred for js gcd)

Same math, while loop instead of recursion:

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

console.log(gcdIter(48, 18));
console.log(gcdIter(-12, 18));
console.log(gcdIter(0, 7));
text
6
6
7

3. gcd in javascript for many numbers

Pairwise fold reuses any javascript gcd function for two arguments:

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

function gcdList(nums) {
  return nums.reduce((acc, n) => gcdTwo(acc, n));
}

console.log(gcdList([12, 18, 24]));
text
6

4. gcd in js with BigInt

Use 0n, %, and literals with n—do not mix BigInt and Number in the same arithmetic expression.

javascript
function gcdBig(a, b) {
  a = a < 0n ? -a : a;
  b = b < 0n ? -b : b;
  while (b !== 0n) {
    const t = b;
    b = a % b;
    a = t;
  }
  return a;
}

console.log(gcdBig(120n, 84n));
text
12n

5. Optional: mathjs for gcd js variadic calls

If a dependency is acceptable, math.gcd accepts several arguments (see mathjs docs). Install with npm install mathjs, then:

javascript
const { gcd } = require("mathjs");

console.log(gcd(12, 18, 24));
text
6

This line was captured with mathjs@13 in Node’s CommonJS require form.


Summary

  • GCD is the largest shared divisor; Euclid’s algorithm uses repeated modulo until b === 0.
  • Normalize negatives with Math.abs (or sign checks for BigInt).
  • Fold reduce across an array for more than two operands; mathjs helps when you want variadic ergonomics.

References

Wikipedia and mathjs documentation for gcd javascript algorithms.


Frequently Asked Questions

1. Is it greatest common divisor or denominator?

The correct term is greatest common divisor (GCD)—the largest integer that divides all inputs with no remainder. Denominator is the bottom of a fraction; it is a different concept.

2. How do I write a javascript gcd function for negative inputs?

Normalize with Math.abs on each operand before the Euclidean loop (or take absolute values inside each recursive step). GCD is usually defined for integers up to sign.

3. What is gcd(0, n) in JavaScript?

With the usual Euclidean loop and Math.abs, gcd(0, n) returns abs(n). If both inputs are 0, the result is conventionally 0.

4. Recursive or iterative gcd in javascript?

Both are correct. Iteration avoids deep recursion stacks on very long chains; recursion is often shorter to read.

5. How do I compute gcd for more than two numbers?

Fold pairwise: gcd(a,b,c) === gcd(gcd(a,b), c). The associative property of GCD makes this order-independent.

6. Can I use BigInt for javascript greatest common divisor?

Yes—the same modulo Euclidean algorithm works with 0n and bigint literals; keep operands as BigInt end-to-end.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital …

  • JavaScript
  • Web Design