# Find all prime factors of a number - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

#### JavaScript for beginners

Best Seller

74 Lectures 10 hours

#### Modern Javascript for Beginners + Javascript Projects

Most Popular

112 Lectures 15 hours

#### The Complete Full-Stack JavaScript Course!

Best Seller

96 Lectures 24 hours

We are required to write a JavaScript function that takes in a number and returns an array of all the prime numbers that exactly divide the input number.

For example, if the input number is 18.

Then the output should be −

const output = [2, 3];

## Example

Let’s write the code for this function −

const num = 18;
const isPrime = (n) => {
for(let i = 2; i <= n/2; i++){
if(n % i === 0){
return false;
}
};
return true;
};
const findPrimeFactors = num => {
const res = num % 2 === 0 ?  : [];
let start = 3;
while(start <= num){
if(num % start === 0){
if(isPrime(start)){
res.push(start);
};
};
start++;
};
return res;
};
console.log(findPrimeFactors(18));

## Output

The output in the console: −

[2, 3]