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.
The result of the function if the condition is true, otherwise undefined.
Example
// Will execute the function and return the result because the condition is true constresult = doIf(true, () =>"Hello, world!"); console.log(result); // "Hello, world!"
Example
// Will not execute the function and return undefined because the condition is false constresult = doIf(false, () =>"This will not run"); console.log(result); // undefined
Executes a function if the condition is
true. If the condition isfalse, returnsundefined. This is a concise way to run code conditionally without using a fullifstatement.