JavaScript call, apply, and bind explained

🎯 Interview Question #7 — 🔥 call vs apply vs bind(Explained So Clearly You’ll Never Forget It!) Most developers use functions… But very few truly understand how to control this in JavaScript. These 3 methods separate juniors from seniors 👇 🔥 1️⃣ call() — Invoke Immediately (Pass Arguments Normally) call runs the function instantly, with a given this. function greet(a, b) { console.log(this.name, a, b); } greet.call({ name: "Rohit" }, "Hello", "World"); ✔ passes arguments one by one ✔ invokes right now 🔥 2️⃣ apply() — Invoke Immediately (Pass Arguments as Array) Same as call(), but arguments must be an array. greet.apply({ name: "Rohit" }, ["Hello", "World"]); Use when your data is already in an array. ✔ invokes immediately ✔ best for array argument lists 🔥 3️⃣ bind() — Returns a New Function (Does NOT Invoke) bind() does NOT run the function. It returns a new function with this permanently attached. const fn = greet.bind({ name: "Rahul" }, "Hello", "World"); fn(); // runs later Best for: callbacks event handlers React class components delayed execution #JavaScript #JS #WebDevelopment #FrontendDevelopment #ReactJS #NextJS #CodingTips #TechTips #InterviewPreparation #FrontendInterview #JavaScriptInterview #WebDevCommunity #LearnJavaScript #ProgrammingTips #TechCareers #CodeNewbie #DevelopersCommunity #SoftwareEngineering #100DaysOfCode #DevLife

To view or add a comment, sign in

Explore content categories