FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Use Previous

10mins

Implement a function usePrevious(value) that stores and returns the previous value of a variable.

The function should:

  • Accept a current value.
  • Return the previous value that was passed in the previous call.
  • Return undefined if called for the first time.

Examples

const prev = usePrevious(1); // undefined const prev2 = usePrevious(2); // 1

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests