Understanding "this" as a core (Arrow vs Normal Function) Many times confusion around this comes from thinking it depends on where a function is written, but in reality it depends on how the function is called. In the example below, the outer function is called as an object method, so its this refers to the object itself. The inner function is an arrow function, which does not create its own this and instead lexically inherits it from the parent function. This small distinction cleared a lot of confusion for me around this behavior in JavaScript . If the inner function were a normal function instead of an arrow function, the value of this would be different. #JavaScript #LearningInPublic #WebDevelopment #Frontend #NodeJS
JavaScript this Keyword Behavior in Arrow vs Normal Functions
More Relevant Posts
-
🔁 Closures in JavaScript A closure is when a function remembers variables from its parent function, even after the parent function has finished executing. function outer() { let count = 0; return function inner() { count++; console.log(count); } } const counter = outer(); counter(); // 1 counter(); // 2 👉 inner() still remembers count 👉 That memory is called a closure If closures ever confused you, save this post 👍 #JavaScript #WebDevelopment #Frontend #CodingSimplified #LearnJS #BhaviDigital
To view or add a comment, sign in
-
-
Closures in JavaScript — Explained Simply A lot of people use closures without actually understanding them. So let’s kill the confusion. What’s a Closure? A closure is just a function that remembers the variables from where it was created, even after that outer function has finished running. Think of it like this The inner function carries a backpack Inside that backpack → variables from the outer scope. #JavaScript #Closures #WebDevelopment #Frontend #ReactJS #LearningInPublic #JavaScriptBasics
To view or add a comment, sign in
-
-
📌 call, apply, bind in JavaScript These methods are used to control the value of this inside a function. 🔹 call() Calls a function immediately and sets this. Use case: Borrow a function from another object. 🔹 apply() Same as call, but arguments are passed as an array. Use case: Useful when arguments are already in an array. 🔹 bind() Returns a new function with this permanently bound. Use case: Used in callbacks, event handlers, and async code. Thanks to - aka Anshu Pandey #JavaScript #WebDevelopment #Frontend #JSConcepts #Developers
To view or add a comment, sign in
-
-
Using array index as a key in React works… until it doesn’t ⚠️ At first, everything looks fine. No errors. No warnings. But the moment you: - insert an item - remove one - or reorder the list state starts jumping between components. This happens because React uses the key to decide component identity. Index is not identity. It’s position. If the order changes, React gets confused — and the bug is silent. If a list can change, the index is almost never the right key. What’s the worst bug you’ve seen caused by this? #React #Frontend #JavaScript #WebDevelopment #ReactJS
To view or add a comment, sign in
-
𝐃𝐚𝐲 𝟒 𝐨𝐟 𝐮𝐧𝐟𝐨𝐥𝐝𝐢𝐧𝐠 𝟙𝟝 𝐦𝐚𝐣𝐨𝐫 𝐜𝐨𝐧𝐜𝐞𝐩𝐭𝐬 𝐨𝐟 𝐑𝐞𝐚𝐜𝐭 One of the main reasons we use React is that we don’t manually interact with the DOM. React manages updates efficiently on our behalf. Instead of directly re-rendering the real DOM, React uses the 𝐕𝐢𝐫𝐭𝐮𝐚𝐥 𝐃𝐎𝐌 for better performance. React provides special functions called 𝐇𝐨𝐨𝐤𝐬 that control component behavior and DOM interaction. One important hook is 𝐮𝐬𝐞𝐒𝐭𝐚𝐭𝐞. It is used to store and manage data (state) inside a component. When the state value changes, React automatically re-renders the component and updates only the necessary parts of the DOM. Learning one concept at a time to build a strong foundation in React. #ReactJS #WebDevelopment #LearningInPublic #ReactHooks #VirtualDOM #Frontend #JavaScript
To view or add a comment, sign in
-
-
● Debounce vs Throttle in React JS ● # Debounce :- What it does:- Executes the function only after the user stops triggering the event for a specific time. Best use cases: Search box (API call after typing stops) Form validation Auto-save feature. # Throttle :- What it does:- Executes the function at regular intervals, even if the event keeps firing. Best use cases: Scroll events Window resize Button click protection (prevent multiple ). #ReactJS #JavaScript #WebDevelopment #Frontend
To view or add a comment, sign in
-
"useEffect" is one of the most misunderstood hooks in React. Most beginners think it’s for “running code after render” but that mindset causes bugs and unnecessary re-renders. A better way to think about "useEffect" It synchronizes your component with something outside React. Good use cases: Fetching data from an API Subscribing to events Updating document.title Cleaning up listeners Rule of thumb: If your logic doesn’t interact with the outside world, it probably doesn’t belong in "useEffect". Understanding this early makes your React code cleaner, predictable, and easier to maintain. #React #WebDevelopment #Frontend #JavaScript #ReactHooks
To view or add a comment, sign in
-
-
A mistake I made while working with React. For a long time, I thought more state meant more control. That assumption caused issues later. Here’s what I noticed in real projects: - Components became harder to reason about - Unnecessary re-renders appeared - Debugging took longer than expected What changed my approach: - I questioned whether state was actually needed - I avoided storing derived values - I kept state close to where it’s used One simple rule helped me a lot. If state can be calculated, it probably shouldn’t be stored. How do you decide what truly belongs in state? #react #javascript #frontend #webdevelopment
To view or add a comment, sign in
-
𝐃𝐚𝐲 #5 — 𝐇𝐨𝐰 𝐑𝐞𝐚𝐜𝐭 𝐇𝐚𝐧𝐝𝐥𝐞𝐬 <input> Many developers think React or JavaScript fully controls the <input> element but that’s not completely true. By default, the browser manages the input UI, updating it on every keystroke. This is known as an uncontrolled component. React can take control by storing the value using state or refs, turning it into a controlled component. 📌 Simple idea: Browser controls input by default. React controls it when needed. A small concept, but powerful for understanding: ✓ Forms ✓ Controlled vs Uncontrolled Components ✓ DOM vs Virtual DOM Learning React one concept at a time. #ReactJS #JavaScript #WebDevelopment #LearningInPublic #Frontend #ReactConcepts
To view or add a comment, sign in
-
Understanding Array.reduce() in JavaScript 👨💻 A simple example of how reduce() helps calculate the total order amount from an array of objects clean, readable, and powerful. Once it clicks, you’ll start seeing reduce() everywhere. #JavaScript #WebDevelopment #Frontend #CodingTips #ReactJS #LearnToCode
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