FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Use Default

10mins

Implement a function useDefault(value, defaultValue) that returns the given value unless it is null or undefined, in which case it returns defaultValue.

The function should:

  • Accept any value and a default.
  • Return the default only if the value is nullish (null or undefined).

Examples

useDefault(5, 10); // 5 useDefault(null, 10); // 10 useDefault(undefined, 42); // 42 useDefault("Hello", "Hi"); // "Hello"

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests