FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Use Toggle

10mins

Implement a function useToggle(initialValue) that manages a boolean state and allows toggling it.

The function should:

  • Accept an initial boolean value.
  • Return an object containing:
    • value: current boolean state.
    • toggle: function that switches the state from true to false or false to true.
    • setTrue: function to set the state to true.
    • setFalse: function to set the state to false.

Example

const toggle = useToggle(false); toggle.value; // false toggle.toggle(); toggle.value; // true toggle.setFalse(); toggle.value; // false

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests