code to check if number is prime number I have written a code to check if the number is prime number are not.

I pass the number from command line argument, and check if the number is prime.

I have written a function for it which returns the boolean value.

(function(){
    'use strict';

    main();

    function main(){
        var testNum = getCommandLineVariables();
        console.log("Is Number \""+testNum+"\" a prime number ? ",isPrimeNumber(testNum));
    }


    function isPrimeNumber(num){
        var halfNum = num/2;
        halfNum = parseInt(halfNum);

        // check if number is divisible by 2, 
        // if yes then its not a prime number.
        if(num%2 === 0){
            return false;
        }


        do{
            // if number is divisible by its half value, 
            // if yes, than its not prime number    
            if(num%halfNum === 0){
                return false;
            }

        }while(--halfNum &gt;= 2); // reduce the half-value by 1 and continue

        return true;
    } // end of isPrimeNumber

    // get the number from commandline argument
    function getCommandLineVariables(){
        var arg_array = process.argv.slice(2);

        if(arg_array.length !== 1){
            throw new Error("Pass a number");
        }


        return parseInt(arg_array[0]);
    } // end of getCommandLineVariables 
})();


I run the program as follows,

E:\math&gt;node isPrimeNum01.js 13
Is Number "13" a prime number ?  true

E:\math&gt;node isPrimeNum01.js 16
Is Number "16" a prime number ?  false

E:\math&gt;node isPrimeNum01.js 29
Is Number "29" a prime number ?  true

E:\math&gt;node isPrimeNum01.js 113
Is Number "113" a prime number ?  true

E:\math&gt;node isPrimeNum01.js 127
Is Number "127" a prime number ?  true

E:\math&gt;node isPrimeNum01.js 566
Is Number "566" a prime number ?  false

E:\math&gt;


Please review my code and leave your valuable comments. Also please check whether my logic for checking the prime number is efficient.
I will just be looking at your prime function.

You got the right idea, but you missed some edge cases, and it can also be optimized. 2 and 3 are prime but return false, since halfNum starts at 1, and do...while loops always run at least once. 1 is not prime but return true, along with some negative numbers. The easiest way to handle this is just to check for this special case at the start of the function.

if(n &lt;= 1) return false;


You are checking for divisibility with all numbers between 2 and half the input, which is good. But it seems a bit confusing to me when you use a do...while loop and counting down, instead of just counting up with a good old for loop.

for(let i = 2; i &lt;= halfNum; i++)


But we can optimize this, since we only need to check up to the square root of the number. For any divisible number larger than the square root, there will also be a divisible number lower than the square root. For example, 100 is divisible by 20, but also 5, since 20*5=100. I'm also going to pre-calculate it and rename halfNum as that name doesn't fit anymore.

const limit = Math.sqrt(num);


The final function looks like this.

function isPrime(num) {
    if(num &lt;= 1) return false;
    const limit = Math.floor(Math.sqrt(num));
    for(let i = 2; i &lt;= limit; i++) {
        if(num % i === 0) return false;
    }
    return true;
}