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
Understanding call, apply, and bind in JavaScript
More Relevant Posts
-
🚨 7 JavaScript Facts That Will Blow Your Mind 🤯 Think you know JavaScript? Wait till you see these 👇 1️⃣ NaN !== NaN 👉 Not even equal to itself 2️⃣ [] + [] = "" 👉 Empty arrays become an empty string 3️⃣ null + 1 = 1 👉 null is converted to 0 4️⃣ typeof null === "object" 👉 This is a bug in JavaScript 5️⃣ 0.1 + 0.2 !== 0.3 👉 Floating point precision issue 6️⃣ == vs === 👉 Always prefer === (strict equality) 7️⃣ JavaScript is single-threaded 👉 Handles async using the event loop 💡 One line to remember: 👉 “JavaScript is simple… until it’s not 😏” 💬 Which fact surprised you the most? 📌 Save this for interviews & quick revision #javascript #webdevelopment #frontend #coding #programming #javascriptdeveloper #learncoding #developers #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 **Understanding JavaScript Variables Like a Pro (var vs let vs const)** If you're working with JavaScript, choosing the right keyword — `var`, `let`, or `const` — is more important than you think. Here’s a simple breakdown 👇 🔸 **var** * Function scoped * Can be re-declared * Can be re-assigned * Hoisted with `undefined` 👉 Mostly avoided in modern JavaScript due to unexpected behavior. --- 🔹 **let** * Block scoped * Cannot be re-declared in same scope * Can be re-assigned * Hoisted but in Temporal Dead Zone (TDZ) 👉 Best for variables that will change. --- 🔒 **const** * Block scoped * Cannot be re-declared * Cannot be re-assigned * Must be initialized at declaration 👉 Best for constants and safer code. --- 💡 **Pro Tip:** Always prefer `const` by default → use `let` when needed → avoid `var`. --- 📊 The attached diagram explains: * Scope hierarchy (Global → Function → Block) * Memory behavior * Key differences visually --- 🔥 Mastering these fundamentals helps you: ✔ Write cleaner code ✔ Avoid bugs ✔ Crack interviews easily --- #JavaScript #WebDevelopment #Frontend #Coding #Programming #Developers #LearnToCode #Tech #SoftwareEngineering #NodeJs #Json
To view or add a comment, sign in
-
-
🚀 JavaScript Last-Minute Cheat Sheet — Perfect Before Interviews & Coding Rounds 🔥 Variables & Scope • "let" & "const" over "var" • Block scope matters • Avoid global variables ⚡ Functions • Arrow functions → shorter syntax • Default parameters save time • Closures = function + lexical scope 🧠 Async JavaScript • Callbacks → Promises → "async/await" • Use "try...catch" for error handling • "Promise.all()" for parallel execution 📦 Arrays & Objects • "map()", "filter()", "reduce()" = must know • Spread "..." & Destructuring simplify code • Optional chaining "?." prevents crashes 🎯 DOM & Events • "querySelector()" is your best friend • Event delegation improves performance • Debounce & throttle for optimization 💡 Pro Tip Understand concepts, not just syntax. Clean code + problem-solving mindset = real JavaScript mastery. Follow Lakhan Soni for more. Cr - Respected Owner Learn more From w3schools.com JavaScript Mastery✨ #WebDevelopment #FrontendDeveloper #CodingInterview #100DaysOfCode #javascript #html #programming #coding #css #java #python #programmer #developer #webdevelopment hashtag #webdeveloper hashtag #coder hashtag #code hashtag #php hashtag #webdesign #softwaredeveloper #computerscience #software #reactjs #technology #frontend #development #tech #linux #javascriptdeveloper #frontenddeveloper #programmers #softwareengineer #web
To view or add a comment, sign in
-
Day 9 / 30 - Javascript Coding Problem: Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained by executing the fn function on each element of the array, sequentially, passing in the return value from the calculation on the preceding element. This result is achieved through the following operations: val = fn(init, nums[0]), val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been processed. The ultimate value of val is then returned. If the length of the array is 0, the function should return init. Please solve it without using the built-in Array.reduce method. Solution: var reduce = function (nums, fn, init) { let numsLength = nums?.length; if (numsLength === 0) { return init; } let acc = init; for (let k = 0; k < numsLength; k++) { acc = fn(acc, nums[k]); } return acc; }; #JavaScript #DSA #CodingPractice #100DaysOfCode #FrontendDevelopment #ProblemSolving
To view or add a comment, sign in
-
-
Hello Connections! 👋 This JavaScript trick confuses 90% of beginners — will you get it right? console.log("5" - 2); // ? console.log("5" + 2); // ? Most people expect similar behavior… but JavaScript has other plans 😅 Let’s break it down: 👉 "5" - 2 JavaScript converts "5" (string) into a number automatically Result: 3 👉 "5" + 2 Here, + acts as string concatenation, not addition Result: "52" 💡 Why does this happen? Because of Type Coercion in JavaScript: - ➝ forces numeric conversion + ➝ prefers string concatenation if one operand is a string Real Lesson: JavaScript is powerful… but also tricky. If you don’t understand type coercion, bugs will find you before you find them. 😄 📌 Save this post — you’ll definitely face this in interviews or real projects. hashtag #JavaScript #JS #FrontendDevelopment #CodingTips #Programming #WebDevelopment #CodeNewbie #TechLearning #InterviewPrep #DevelopersLife #python
To view or add a comment, sign in
-
-
Understanding call(), apply(), and bind() is essential for mastering JavaScript — especially when working with the this keyword. Many developers struggle with: • When to use call() vs apply() • Why bind() behaves differently • How these methods work in real-world scenarios I’ve created a detailed, easy-to-understand guide covering: ✔ Core concepts ✔ Practical examples ✔ Key differences ✔ Interview-focused explanations If you're learning JavaScript or preparing for interviews, this will help you a lot. Read the full article here: https://lnkd.in/gKviFuj2 Would love to hear your thoughts — which one do you use most in your projects? #JavaScript #WebDevelopment #FrontendDevelopment #Programming #SoftwareDevelopment #Coding #TechLearning
To view or add a comment, sign in
-
🚀 JavaScript Array Methods You Should Know If you’re still looping through arrays manually, there’s an easier way 👇 These array methods can make your code cleaner, faster to read, and easier to maintain: ✅ filter() → Get matching items ✅ map() → Transform data ✅ find() → Get the first match ✅ some() → Check if at least one item matches ✅ every() → Check if all items match ✅ includes() → Check if a value exists ✅ findIndex() → Find the position of an item ✅ push() / pop() → Add or remove items 💡 Pro tip: In React, map() and filter() are used a lot for rendering lists and handling data properly. Mastering these methods can help you write better code and do better in interviews 💻✨ Which array method do you use most often? 🤔 #JavaScript #WebDevelopment #ReactJS #FrontendDevelopment #Programming #Coding #Developer #SoftwareDevelopment #LearnToCode #CodingTips
To view or add a comment, sign in
-
-
JavaScript Scope & Closure — Concepts You MUST Know 💡 Understanding scope and closure is key to mastering JavaScript . 🔹 Scope determines where variables are accessible. Global Scope Function Scope Block Scope (let & const) 🔹 Closure is when a function “remembers” variables from its outer scope even after the outer function has finished execution. 👉 Simple Example: A function inside another function can access the parent function’s variables — that’s closure in action. 📌 Why it matters: Helps in data hiding (encapsulation) Used in callbacks, event handlers, and async code Essential for writing clean and efficient code 🚀 If you're preparing for interviews or building projects, mastering these concepts will level up your JavaScript skills. #JavaScript #WebDevelopment #FrontendDevelopment #Coding #Programming #MERNStack #InterviewPreparation #LearnToCode #Developers #TechSkills
To view or add a comment, sign in
-
JavaScript Interview Series – Day 14 Let’s understand What is Prototype in JavaScript. In JavaScript, every object has a hidden property called [[Prototype]] which links to another object. This is called the prototype chain. It allows objects to inherit properties and methods from other objects. 🔎 Key Concept • Objects inherit from other objects • Prototype enables code reuse • JavaScript uses prototype-based inheritance #JavaScript #NodeJS #WebDevelopment #Programming #SoftwareEngineering #InterviewPreparation
To view or add a comment, sign in
-
-
Most developers learn Objects first in JavaScript… But very few truly understand when to use Map instead of Object. And that’s where small mistakes start affecting performance and code quality. I’ve written a detailed yet easy-to-understand guide on: • Key differences between Map and Object • Why Map handles keys more efficiently • Iteration and performance comparison • Practical use cases you’ll face in real projects If you're serious about improving your JavaScript fundamentals or preparing for interviews, this is worth your time. 👉 Read the full article here: https://lnkd.in/gDyB7vap Let me know your thoughts — do you use Map in your projects? #JavaScript #FrontendDevelopment #WebDevelopment #Programming #SoftwareDevelopment #Coding #Developers #TechCareers #LearnJavaScript
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development