PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » JavaScript » Check if Number is Even/Odd

Check if Number is Even/Odd

function isEven(value) {
	if (value%2 == 0)
		return true;
	else
		return false;
}

Subscribe to The Thread

  1. Red says:

    Why not just

    function isEven(value) {
    return (value%2 == 0);
    }

  2. Robin says:

    And the PHP version only needs an extra dollar sign:

    function isEven($int){
    return ($int%2 == 0);
    }

    In these times, I can assume most webdevvers use a javascript library like jQuery or similar. These libraries have functions like isEven built in.

    For those who don’t use such libraries: great snippet ;)

  3. Gautham says:

    I came across http://rindovincent.blogspot.com/p/javascript.html where there was a simple Javascript program to find whether the number is odd or even. I am pasting the same code with permission here.

    var n = prompt(“Enter a number to find odd or even”, “Type your number here”);
    n = parseInt(n);
    if (isNaN(n))
    {
    alert(“Please Enter a Number”);
    }
    else if (n == 0)
    {
    alert(“The number is zero”);
    }
    else if (n%2)
    {
    alert(“The number is odd”);
    }
    else
    {
    alert(“The number is even”);
    }

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~