Q: Check if a number is prime in JavaScript

+4 votes

Write a JS function to check if a number is prime (only wholly divisible by itself and one). The input comes as a single number argument.


Input: 81

Output: false

Input: 7

Output: true

Input: 8

Output: false

The output should be the return value of your function. Return true for prime number and false otherwise.

asked in JavaScript category by user icabe
recategorized by user golearnweb

2 Answers

+4 votes

Here is my Javascript script:

function isPrime(input) {
    let prime = true;
    for (let i = 2; i <= Math.sqrt(input); i++) {
        if (input % i == 0) {
            prime = false;
    return prime && (input > 1);



answered by user richard8502
+2 votes
Here is video:

answered by user sam