JavaScript interview questions
Show answer
function debounce(fn, ms) { let t; return (...args) => { clearTimeout(t); t = setTimeout(() => fn(...args), ms); }; }Debounce runs the function once activity stops for
ms(search-as-you-type). Throttle runs it at most once everymswhile activity continues (scroll position tracking). Mention cancelling on unmount and preservingthisif needed.