Facebook Pixel
How the sum(1)(2)(3) Function Uses Closures

Does each call to sum create a new closure?

The answer

Yes. Each sum(a) call creates a new function with a new closure over a. The previous closure is replaced, but the accumulated value (a + b) is passed forward to the new sum call.

More FAQs in How the sum(1)(2)(3) Function Uses Closures

Each call to sum(a) returns a function that closes over a. When called with b, it returns sum(a + b), creating a new closure with the accumulated total. The closure keeps the running sum alive between calls.

b is undefined. The function checks if (b !== undefined), which is false. It returns a (the accumulated sum). This is how the chain terminates and the result is retrieved.

No. The curried sum requires each returned function to remember the accumulated total. This is only possible with closures. Without closures, the inner function would not have access to the previous arguments.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.