How To Check Number Is Odd Or Even

Education Here is a quick little snippet which can prove to be quite useful.

This snippet will check to see if a number is odd or even.

In Javascript

function checkEven(val){
     return (val%2 == 0);

val = 3;

    alert("Number is even");
} else {
    alert("Number is odd");


In PHP it's actually exactly the same just fix the variables.

function checkEven($val){
     return ($val%2 == 0);

$val = 3;

    echo "Number is even";
} else {
    echo "Number is odd";
Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial