Find if variable is divisible by 2

前端 未结 12 506
梦谈多话
梦谈多话 2020-12-07 19:22

How do I figure out if a variable is divisible by 2? Furthermore I need do a function if it is and do a different function if it is not.

12条回答
  •  一生所求
    2020-12-07 20:21

    You can do it in a better way (up to 50 % faster than modulo operator):

    odd: x & 1 even: !(x & 1)

    Reference: High Performance JavaScript, 8. ->Bitwise Operators

提交回复
热议问题