Grow your CSS skills. Land your dream job.

Last updated on:

Check if Number is Even/Odd

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

Comments

  1. Red
    Permalink to comment#

    Why not just

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

    • meher
      Permalink to comment#

      printing it whether it z odd or even is equally important…!!! but the logic is quite good.. sweet and simple..!!

  2. Permalink to comment#

    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. 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”);
    }

  4. Miljenko
    Permalink to comment#

    function isEven(value)
    {
    return /^\d+$/.test(value.toString()) ? (value%2 === 0 ? true : false ) : false;
    };

  5. x.Flowwolf
    Permalink to comment#

    //so, my turn =)
    function isEven(value){
    return isFinite(+value) && !(+value % 2)
    }

  6. Pj
    Permalink to comment#

    function isOdd(n){
    return 1&n;
    }

    Do i win?

  7. Permalink to comment#

    start
    read n
    reminder=n/2
    if (reminder=0)
    print”number is even”
    else
    print”number is odd”
    stop

  8. I’ve read that the == comparison is dangerous because it does type coercion to make comparisons so it’s better practice to use === which will return true if both the left/right side are truly identical.

    • Evan
      Permalink to comment#

      Generally, it’s best practice to use === but in this example, value % 2 is guaranteed to return an integer or NaN, which allows us to absolutely know the type of variable we’re comparing on either side of num % 2 or 0, but not num itself.

      The problem comes down to if you accidentally get a false or true as an input, which loosely converts to a 0 or 1 when using the % operator. This might cause some problems in your code if false returns true. You normally would use a equality (===) operator to make this comparison, but since the boolean gets converted to a number on the % operator, equality won’t work here as most have typed the function.

      Given the unknown nature of the use for this function and given it’s name, I’d say the following would be safe to use:

      function isEven(num) {
        return isNaN(num) && num !== false && num !== true ? false : num % 2 == 0;
      }
      

      or for those who enjoy readable code:

      function isEven(num) {
        if(num !== false && num !== true && !isNaN(num)) {
          return num % 2 == 0;
        } else return false;
      }
      
    • Tony

      @Evan hit it right on the head

      also not to mention == is faster than ===, so if you know what you are doing it’s faster to use
      the == when applicable but if you are not sure, use ===

      Cheers

  9. include<stdio.h>

    include<conio.h>

    main()
    {
    int n;
    clrscr();
    printf(“Enter a Number:\n”);
    scanf(“%d”,&n);
    if(n%2==0)
    {
    printf(“Even Number”);
    }
    else
    {
    printf(“Odd Number”);
    }
    getch();
    }

  10. Odd And Even syntax

  11. And what if the “value” isn’t a base 10 number?

    function isEven(value) {
     value.toString(10)%2==0?true:false;
    }
    
  12. Raniya A.
    Permalink to comment#

    var a;
    var b=”ODD”;
    var c=”EVEN”;
    a=parseInt(window.prompt(“Enter The Value:”));
    document.write(“Your Value is: “,chk(a));

    function chk(a)
    {
    if (a%2==0)
    return c;
    else
    return b;
    }

  13. Olmo Kramer

    function isEven(value) {
    return !x%2;
    }

  14. Johnny Boy
    Permalink to comment#

    Here’s a blog article which benchmarks quite a few ways to test if a number is odd or even:
    http://blogs.davelozinski.com/curiousconsultant/csharp-net-fastest-way-to-check-if-a-number-is-odd-or-even

    There are actually numerous ways to do this other than the two you have listed. Surprisingly, the fastest way appears to be the modulus % operator, even out performing the bitwise ampersand &, as follows:

    if (x % 2 == 0)
        total += 1; //even number
    else
        total -= 1; //odd number
    

    Definitely worth a read for those that are curious.

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like this:

```
<div>Example code</div>
```

You don't need to escape code in backticks, Markdown does that for you. If anything screws up, contact us and we can fix it up for you.

*May or may not contain any actual "CSS" or "Tricks".