0
How can ich check if a number is un/even? JavaScript!!
I have the number 12 how can I check if this number is even / odd?
4 Answers
+ 2
var testNumber=34;
if (testNumber%2==0){
alert("even number");
}else{
alert("odd number");
}
+ 3
Another solution is to use binary operators ( and accessory using the ternary one for doing the conditional statement ) :
alert( ( test_nb & 1 ) ? "odd" : "even" );
"And" ( & ) binary operator provide kind of masking, so ( n & 1 ) keep only the most right bit ( in binary representation ), so give same result as ( n % 2 ) but in a more efficient way...
0
Yes the same thing I tested right now in this second but thank you!
0
Use modulus:
function even(n) {
return n % 2 == 0;
}
function isodd(n) {
return n % 2 != 0;
}
isodd(15) //true
even(10) //true