Understanding call, apply, and bind in JavaScript

Call, Apply, and Bind in JavaScript Understanding "this" in JavaScript can be tricky. That’s where call, apply, and bind come in. They allow you to control what this refers to. Here’s the difference: • call() → Invokes the function immediately, arguments passed individually • apply() → Invokes immediately, arguments passed as an array • bind() → Returns a new function with this bound (can be called later) Example: const obj = { name: "Tejas" }; function greet(message) { console.log(`${message}, ${this.name}!`); } greet.call(obj, "Hello"); // Hello, Tejas greet.apply(obj, ["Hi"]); // Hi, Tejas const newFn = greet.bind(obj); newFn("Namaskar"); // Namaskar, Tejas Why this matters: • Helps in function borrowing • Useful in event handlers • Commonly asked in interviews • Strengthens understanding of core JavaScript #JavaScript #WebDevelopment #FrontendDevelopment #Coding #Programming

To view or add a comment, sign in

Explore content categories