- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# Function to calculate the least common multiple of two numbers in JavaScript

The least common multiple, (LCM) of two integers a and b, is the smallest positive integer that is divisible by both a and b.

**For example −**

LCM of 4 and 6 is 12 because 12 is the smallest number that is exactly divisible by both 4 and 6.

We are required to write a JavaScript function that takes in two numbers, computes and returns the LCM of those numbers.

## Example

Following is the code −

const num1 = 4; const num2 = 6; const findLCM = (num1, num2) => { let hcf; for (let i = 1; i <= num1 && i <= num2; i++) { if( num1 % i == 0 && num2 % i == 0) { hcf = i; }; }; let lcm = (num1 * num2) / hcf; return lcm; }; console.log(findLCM(num1, num2));

## Output

Following is the output on console −

12

- Related Articles
- Write a program to calculate the least common multiple of two numbers JavaScript
- Finding the least common multiple of a range of numbers in JavaScript?
- Smallest Common Multiple of an array of numbers in JavaScript
- Haskell Program to calculate the Lowest Common Multiple
- C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)
- Calculating least common of a range JavaScript
- Checking if decimals share at least two common 1 bits in JavaScript
- How to calculate GCD of two or more numbers/arrays in JavaScript?
- Function to check two strings and return common words in JavaScript
- Common element with least index sum in JavaScript
- Java program to calculate the product of two numbers
- Calculating the LCM of multiple numbers in JavaScript
- Print the kth common factor of two numbers
- PHP program to calculate the repeated subtraction of two numbers
- C++ Program for the Common Divisors of Two Numbers?

Advertisements