@pathmaker-digital/pmd-utilities - v2.2.6
    Preparing search index...

    Function doIf

    • Executes a function if the condition is true. If the condition is false, returns undefined. This is a concise way to run code conditionally without using a full if statement.

      Type Parameters

      • T

      Parameters

      • cond: boolean

        The condition to check.

      • fn: () => T

        The function to execute if the condition is true.

      Returns undefined | T

      The result of the function if the condition is true, otherwise undefined.

      // Will execute the function and return the result because the condition is true
      const result = doIf(true, () => "Hello, world!");
      console.log(result); // "Hello, world!"
      // Will not execute the function and return undefined because the condition is false
      const result = doIf(false, () => "This will not run");
      console.log(result); // undefined