🔹 Why call, apply, and bind exist In JavaScript, this keyword is not fixed. Its value depends on how a function is called, not where it’s written. call, apply, and bind let you manually control what this refers to. Here’s the simplest way to remember them 👇 call() • Invokes a function immediately • Passes arguments one by one fn.call(context, arg1, arg2) apply() • Invokes a function immediately • Passes arguments as an array fn.apply(context, [arg1, arg2]) bind() • Does NOT invoke immediately • Returns a new function with bound context const newFn = fn.bind(context) 👉 Key takeaway • call & apply → execute now • bind → execute later Understanding these helps you: • Control this • Write reusable functions • Debug tricky JavaScript issues Which one confused you the most when you started? #JavaScript #FrontendDevelopment #React #WebDev #JSFundamentals #LearningInPublic
Mastering call, apply, and bind in JavaScript
More Relevant Posts
-
🚀 Callbacks in JavaScript — Don’t Call Me Now, Call Me Later A callback is simply a function 👉 passed into another function 👉 and executed after a task is completed This is how JavaScript handles async operations. 🧠 Why Callbacks Exist JavaScript is single-threaded, but not blocking. Callbacks allow long tasks (API calls, timers, events) to finish without stopping execution. 📌 What’s Happening Here? ✔️ fetchData starts an async task ✔️ JavaScript moves ahead without waiting ✔️ After 1 second, the callback is pushed to the call stack ✔️ Callback runs when the stack is empty 🎯 Why This Topic Matters ✅ Foundation of async JavaScript ✅ Explains event handling & timers ✅ Leads to promises & async/await ✅ Common interview question 💡 Reality Check: “Callbacks are powerful… until they turn into callback hell.” #JavaScript #Callbacks #AsyncJavaScript #Frontend #WebDevelopment #JSConcepts #InterviewPrep
To view or add a comment, sign in
-
-
🟨 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗗𝗮𝘆 𝟰𝟴: 𝗔𝘀𝘆𝗻𝗰 & 𝗔𝘄𝗮𝗶𝘁 (𝗗𝗲𝗲𝗽 𝗯𝘂𝘁 𝗦𝗶𝗺𝗽𝗹𝗲) Some JavaScript operations take time — and async & await help us handle them cleanly. 🔹 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗮𝘀𝘆𝗻𝗰? • async is a keyword used to create an async function • An async function always returns a Promise • Even a normal return value is wrapped inside a Promise async function getData() { return "JavaScript"; } 🔹 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗮𝘄𝗮𝗶𝘁? • await can only be used inside an async function • It pauses the execution of that function only • JavaScript continues running other code const result = await fetch("/api"); 🔹 𝗛𝗼𝘄 𝗔𝘀𝘆𝗻𝗰/𝗔𝘄𝗮𝗶𝘁 𝗪𝗼𝗿𝗸𝘀 • Code runs normally until it hits await • Function execution is suspended • Once the Promise resolves, execution continues from the same line 🔹 𝗪𝗵𝘆 𝗪𝗲 𝗨𝘀𝗲 𝗔𝘀𝘆𝗻𝗰/𝗔𝘄𝗮𝗶𝘁 • Makes async code look synchronous • Improves readability • Easier error handling with try / catch 🔹 𝗞𝗲𝘆 𝗣𝗼𝗶𝗻𝘁 Async & await do not block JavaScript They only pause the async function — not the event loop. 💬 GitHub link in the comments for examples #JavaScript #Day48 #100DaysOfCode #Frontend
To view or add a comment, sign in
-
🔍 Understanding the this keyword in JavaScript The this keyword in JavaScript often confuses developers — and honestly, that’s normal. 👉 this refers to the object that is currently calling the function. But its value changes based on how the function is called, not where it’s written. 📌 Common cases: 🔹 Global context In the browser, this refers to the window object. 🔹 Inside an object method this refers to the object itself. 🔹 Inside a regular function this depends on how the function is called (can be undefined in strict mode). 🔹 Arrow functions Arrow functions do not have their own this. They inherit this from their parent scope. 🔹 In classes this refers to the instance of the class. 💡 Key takeaway: If you’re ever confused about this, ask yourself: “Who is calling this function?” That question usually clears things up. Learning JavaScript deeply is not about memorizing rules — it’s about understanding how things behave in real scenarios. #JavaScript #WebDevelopment #FrontendDevelopment #FullStackDeveloper #LearningInPublic #JSConcepts
To view or add a comment, sign in
-
The JavaScript start to make sense with the help of Namaste JS. JavaScript is single-threaded. It can only do one thing at a time. So how does it handle timers, API calls, and other async stuff without freezing? Turns out, it doesn't. The browser does. JavaScript hands off async operations to the browser (Web APIs), keeps running your code, and picks up the results later when it's free. The event loop is just the thing that coordinates all of this. The tricky part? Understanding why this prints in this order: setTimeout → goes to callback queue Promise → goes to microtask queue (higher priority) Promises always cut the line. That's why they execute before setTimeout, even with 0ms delay. Documented the whole flow with examples: https://lnkd.in/dC3mh_AV If the event loop still feels like magic, maybe this helps. #JavaScript #WebDev #Coding #LearningInPublic
To view or add a comment, sign in
-
🚀 JavaScript Scope & Lexical Environment — How JS Finds Your Variables Ever wondered how JavaScript knows which variable to use when the same name exists in multiple places? That magic happens because of Scope and the Lexical Environment. 🧠 Scope (In Simple Words) Scope defines where a variable can be accessed. JavaScript follows lexical (static) scoping, meaning: ➡️ Scope is decided by where the code is written, not how it’s called. 🧩 Lexical Environment A Lexical Environment is: A memory space for variables & functions A reference to its parent scope This creates the scope chain. 📌 What’s Happening Here? ✔️ inner() first looks in its own scope ✔️ If not found, it moves to outer() scope ✔️ Then to the global scope ✔️ This lookup path is called the Scope Chain 🎯 Why This Matters in Real Life ✅ Foundation of closures ✅ Prevents accidental variable access ✅ Helps write predictable, bug-free code ✅ Common interview favorite topic 💡 Golden Rule: “JavaScript looks for variables from inside → outside, never the other way around.” #JavaScript #Scope #LexicalEnvironment #WebDevelopment #Frontend #JSConcepts #InterviewPrep #ReactJS
To view or add a comment, sign in
-
-
So, you're building projects with JavaScript - and that's awesome. But, let's get real, do you really understand how the "this" keyword works? It's tricky. Its value is all about how a function is called, not where it's defined - that's the key. You see, context is everything here. And, honestly, it's not that hard once you wrap your head around it. The thing is, "this" behaves differently in various contexts - and that's what trips people up. For instance, when you're working with object literals, the "this" keyword refers to the object itself - makes sense, right? But, when you're dealing with function calls, it's a whole different story - thethis keyword can refer to the global object, or even null, depending on how the function is invoked. So, it's essential to understand these nuances to avoid common mistakes. Like, have you ever tried to access a property using "this", only to find out it's undefined? Yeah, that's frustrating. Anyway, if you're just starting out with JavaScript, or revisiting the basics, this post is for you. You'll learn whatthis means in JavaScript, how it behaves in different contexts, and some common pitfalls to watch out for. It's all about mastering the "this" keyword - and trust me, it's worth it. Check out this deep dive: https://lnkd.in/g-tn9CXj #JavaScript #Coding #WebDevelopment
To view or add a comment, sign in
-
JavaScript – Execution Context Before JavaScript runs even a single line of your code, it prepares a working space for it. That space is called an Execution Context. In simple words: Execution Context is where JavaScript remembers things and runs your code Whenever JavaScript runs: ● Your whole program → one big Execution Context ● Every function call → a new small Execution Context Each one is created in two steps: 1️⃣ Memory Phase ● Variables are created → undefined ● Functions are stored fully 2️⃣ Execution Phase ● Code runs line by line ● Variables get real values ● Functions are executed Example: *** var a = 5; function show() { var b = 3; console.log(a + b); } show(); *** How JavaScript works: ● Creates a global context ◦ a → undefined ◦ show → saved ● Runs code ◦ a = 5 ◦ show() is called → new context is created ● Inside show() ◦ b = 3 ◦ Prints 8 JavaScript manages these contexts using a Call Stack: ● Global goes first ● Each function goes on top ● When a function finishes, it is removed This is why understanding Execution Context helps you: ● Understand hoisting ● Read call stack errors ● Master scope & closures ● Debug with confidence This is how JavaScript thinks before it acts. #Day1 #JavaScript #Frontend #WebDevelopment #LearningInPublic #React #Developers #CareerGrowth
To view or add a comment, sign in
-
The this Keyword in JavaScript — One Concept, Many Bugs Most JavaScript issues related to this come from a single misunderstanding: this is not based on where a function is defined. It is based on how the function is invoked. Inside an object method, this correctly refers to the object. Inside callbacks such as forEach, map, or setTimeout, that context is lost — often resulting in undefined. This is why: Hard-coding object names does not scale Callbacks silently break context Arrow functions exist Arrow functions lexically bind this, preserving the surrounding context without bind or workarounds. Key takeaway: If this is undefined, the issue is rarely the variable — it’s the invocation. Wishing everyone a Merry Christmas and a restful holiday season 🎄 #JavaScript #SoftwareEngineering #WebDevelopment #JavaScriptTips #CleanCode #FrontendDevelopment
To view or add a comment, sign in
-
I spent time understanding the difference between the for loop and the for-of loop in JavaScript, and this is my summary; With a for loop, I’m mainly working with indexes and manually controlling the loop. With a for-of loop, I’m working directly with the items themselves, which feels more automatic and readable. Same goal but different approaches. These small concepts actually makes writing JavaScript clearer, knowing what/when to use them. #JavaScript #TechJourney #WebDevelopment #Growth
To view or add a comment, sign in
-
🚀 JavaScript this Keyword — One Word, Many Behaviors this is not about where a function is written It’s about how the function is called. That’s why it confuses even experienced developers. 🧠 How JavaScript Decides this JavaScript determines this at runtime, not during writing the code. 📌 What’s Happening Here? ✔️ Global this depends on environment ✔️ Normal functions get this from the caller ✔️ Object methods point this to the object ✔️ Arrow functions inherit this from lexical scope ✔️ call, apply, bind explicitly set this 💡 Golden Rule: “If it’s an arrow function, this is borrowed — not created.” #JavaScript #ThisKeyword #Frontend #WebDevelopment #JSConcepts #InterviewPrep #ReactJS #MERN
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