Linked Questions
19 questions linked to/from What is a practical use for a closure in JavaScript?
1 vote
2 answers
95 views
How does React's useCallback read the variable in closures [duplicate]
In the following example, when the button is clicked, cb2 uses the memoized function. However, why doesn’t cb2 use the first render closures (countVal: 0)? function TestHook() { const [count, ...
0 votes
0 answers
87 views
what are some common uses of closures? [duplicate]
A closure is basically an inner/nested function. What are some of the most common examples of nested functions in popular js frameworks? In what scenarios would you choose to write a custom closure?
1 vote
0 answers
70 views
How variable scopes work in higher order functions in javascript? [duplicate]
I will use a debounce function as example. const debounce = (func, wait) => { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); ...
0 votes
0 answers
94 views
Throttle Pattern Typescript [duplicate]
const throttle = <T extends unknown[]>( callback: (...args: T) => void, delay: number, ) => { // this is not a global variable, just a local variable ?? let isWaiting = false; ...
17 votes
3 answers
16k views
What are the benefits of a closure, and when are they typically used?
I'm a web developer, but lots of folks are looking for slightly more advanced skills and understanding closures seems to be at the forefront of this. I get the whole "execution context creating a ...
15 votes
4 answers
8k views
Whats the need and use of nested functions in JavaScript
I understand what a nested function is, but I don't understand why we even need nested functions in the first place. Is there a problem that can only be solved by using nested functions in JavaScript. ...
2 votes
2 answers
9k views
Javascript, outside variable scope in callback function?
everyone. I have a problem about the 'callback function - scope of variable', I wanna use the 'i in for loop' to 'the callback function User_UidSearch', but I cannot use it. (I hope the solution ...
4 votes
3 answers
4k views
Specific real life application of closures in javascript
I have used JS for two years and my pluralsight accessment rates me as proficient in JS, I understand prototypical inheritance, higher order functions, IIFEs etc and I have used them in real world ...
1 vote
3 answers
2k views
Callback function cannot access variable within parent function's scope
In the below code, variable text is not accessible in the findTextToDelete function (it generates an error) array = ['a', 'b', 'removeThis', 'c']; removeText("removeThis"); function ...
0 votes
1 answer
3k views
calling multiple setInterval() functions in js with a loop [duplicate]
I'm new to javascript and I'm having trouble using the setInterval() function properly. Basically I want to call a function at different given intervals for different parameters both of which I have ...
0 votes
1 answer
2k views
li input checkbox: how to get checked state javascript
I have access to each of the li[i] that returns what you see in the picture. An example would be li[i].innerText returns business. How can I get the checked state of each element? //loop through ...
0 votes
3 answers
1k views
How to pass data by value into a deferred then handler?
I would like to know how to pass data by value, rather than by reference, into a then handler on a jQuery Deferred object. I have the following example code to illustrate my question: var value; var ...
-1 votes
1 answer
749 views
Javascript: What is the benefit of two sets of parentheses after function call [closed]
I understand that in Javascript a function can return another function and it can be called immediately. But I don't understand the reason to do this. Can someone please explain the reason and benefit ...
0 votes
1 answer
690 views
IIFEs as closures
In the You Don't Know Javascript series, 1/3 of the way down IIFE's are described as not being closures themselves, but only if they're executed outside their lexical scope: Chapter 3 introduced the ...
0 votes
3 answers
858 views
Why does this for / setTimeout() code actually output the numbers from 0 to 9?
A common pitfall with JavaScript closures is running setTimeout() from a for loop, and expecting the counter to be passed with different values at each iteration, while in practice it gets assigned ...