What is binding in JavaScript?

In JavaScript the bind() method creates a new function and sets this to a specific value.

For instance, here we have a simple object:

Then we use the bind() method to set the context of this to the person object:

When it is all done and the function is invoked it will look like this:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store