Skip to content

Instantly share code, notes, and snippets.

@felladrin
Last active March 28, 2024 09:40
Show Gist options
  • Save felladrin/7c7467b8228972985a466ef3a16d2b82 to your computer and use it in GitHub Desktop.
Save felladrin/7c7467b8228972985a466ef3a16d2b82 to your computer and use it in GitHub Desktop.
Knowing if a number is odd in JavaScript without using division.
import { isOdd } from "./isOdd.mjs";
isOdd(7); // returns true
isOdd(8); // returns false
isOdd(9); // returns true
export function isOdd(n) {
return Boolean(n & 1 == 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment