How to determine if a number is odd in JavaScript

后端 未结 27 1773
一向
一向 2020-11-27 10:05

Can anyone point me to some code to determine if a number in JavaScript is even or odd?

27条回答
  •  失恋的感觉
    2020-11-27 10:37

    Use the below code:

    function isOdd(num) { return num % 2;}
    console.log("1 is " + isOdd(1));
    console.log("2 is " + isOdd(2));
    console.log("3 is " + isOdd(3));
    console.log("4 is " + isOdd(4));

    1 represents an odd number, while 0 represents an even number.

提交回复
热议问题