faqts : Computers : Programming : Languages : JavaScript : Language Core : Numbers

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

26 of 33 people (79%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How can I check whether an integer is even?
How can I check whether an integer is odd?

Mar 24th, 2000 10:10
Martin Honnen,


Use the modulo operator % to check
  n % 2 == 0

function isEven (n) {
  return n % 2 == 0;
}
function isOdd (n) {
  return n % 2 != 0;
}


Examples

alert(isEven(10))
alert(isOdd(10))