zhanglinjie_'s blog

By zhanglinjie_, history, 5 years ago, In English

When you want to check is a number even, a simple way is compare the last bit with 0.

In Python you can write:

>>> 42 & 1 == 0
True

But in JavaScript you should take care about operator precedence

42 & 1 == 0
// returns 0

(42 & 1) == 0
// returns true

MDN has a detailed document about all precedence of operators in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table

  • Vote: I like it
  • +11
  • Vote: I do not like it

| Write comment?