GitHub 6248★

Check if a number is even

JavaScript version

const isEven = (n) => n % 2 === 0;

// Or
const isEven = (n) => (n & 1) === 0;

// Or
const isEven = (n) => !(n & 1);

// Or
const isEven = (n) => Number.isInteger(n / 2);

TypeScript version

const isEven = (n: number): boolean => n % 2 === 0;

// Or
const isEven = (n: number): boolean => (n & 1) === 0;

// Or
const isEven = (n: number): boolean => !(n & 1);

// Or
const isEven = (n: number): boolean => Number.isInteger(n / 2);

Examples

isEven(1); // false
isEven(2); // true
Follow me on and to get more useful contents.