Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

isOdd

Checks if a number is odd.

TypeScript
Copied!
1/**
2 * Checks if a number is odd.
3 *
4 * @param num - The number to check.
5 * @returns True if the number is odd, otherwise false.
6 */
7export function isOdd(num: number): boolean {
8  return num % 2 !== 0;
9}
  • Minimal and Fast

    Simple modulus check provides immediate result with negligible performance cost.

  • Readable and Intentional

    The logic clearly conveys its purpose, enhancing code clarity.

  • Reliable Boolean Return

    Always returns a strict true or false, making it safe in control flow and validations.

Tests | Examples

TypeScript
Copied!
1test('isOdd - positive odd number', () => {
2  expect(isOdd(5)).toBe(true);
3});
4
5test('isOdd - positive even number', () => {
6  expect(isOdd(6)).toBe(false);
7});
8
9test('isOdd - zero is not odd', () => {
10  expect(isOdd(0)).toBe(false);
11});
12
13test('isOdd - negative odd number', () => {
14  expect(isOdd(-3)).toBe(true);
15});
16
17test('isOdd - negative even number', () => {
18  expect(isOdd(-4)).toBe(false);
19});
20
21test('isOdd - float number', () => {
22  expect(isOdd(3.1)).toBe(true);
23});

Common Use Cases

  • Conditional Rendering or Logic

    Apply specific styles, rules, or logic to odd-indexed items (e.g., zebra-striping).

  • Turn-Based Systems

    Determine whether it’s an odd-numbered turn in games or simulations.

  • Input Validation

    Enforce odd-number-only constraints for IDs, port numbers, or user-defined rules.

  • Behavior Toggling

    Use odd values to alternate behaviors, such as animation direction or layout flips.

Codebase: Utilities -> Numbers -> isOdd | Yevhen Klymentiev