Facebook Pixel

How do you use bind for partial application in JavaScript?

const add5 = add.bind(null, 5) creates a function where the first argument is always 5. When called with (3, 2), it calls add(5, 3, 2). This is partial application.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Practical Use Cases for call, apply, and bind

Use call: Array.prototype.slice.call(arguments) borrows slice to convert array-like objects to arrays. Array.prototype.forEach.call(nodeList, fn) borrows forEach for NodeLists.

Use bind: setTimeout(obj.method.bind(obj), 1000). Or use an arrow wrapper: setTimeout(() => obj.method(), 1000). bind permanently sets this for the returned function.

Math.max.apply(null, [1, 2, 3]) returns 3. In modern JS, use spread: Math.max(...[1, 2, 3]). Both work, but spread is more readable.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

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
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.