𝗖𝗿𝗮𝗰𝗸 𝘁𝗵𝗲 𝗖𝗼𝗱𝗲: 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗛𝗼𝗶𝘀𝘁𝗶𝗻𝗴 𝗘𝘅𝗽𝗹𝗮𝗶𝗻𝗲𝗱! 🤔 Ever seen code that works even though you call a function before defining it? That's the magic (and potential trap) of Hoisting! 🪄 Here is a simple breakdown of this essential JS concept: What is it? Think of it as the JavaScript engine giving your declarations a "lift." Before running your code, it moves function and variable declarations (not their values!) to the top of their scope. ⬆️ 𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 𝘄𝗶𝘁𝗵 𝘃𝗮𝗿: These are hoisted and initialized to undefined. You can access them, but they won't have their values yet. 🤷♂️ 𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 𝘄𝗶𝘁𝗵 𝗹𝗲𝘁 & 𝗰𝗼𝗻𝘀𝘁: These are hoisted but not initialized. Accessing them before they're defined throws an error—a safe space known as the Temporal Dead Zone (TDZ)! 🛑🚫 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀: Full function declarations are hoisted, allowing you to call them anywhere in their scope. (This is super convenient!) 🤩 Understanding hoisting is crucial for avoiding confusing bugs and writing cleaner, more predictable code. 🧱💻 Check out this diagram for a visual guide! #JavaScript #WebDevelopment #CodingTips #SoftwareEngineering #Hoisting #TemporalDeadZone #LearningToCode #WebDev
Understanding JavaScript Hoisting: A Key Concept for Web Developers
More Relevant Posts
-
I thought JavaScript runs code line by line. It doesn't. Before executing a single line, the JS engine creates something called an Execution Context. Think of it like a stack of plates. Each function call adds a new plate on top. Last plate added is the first one removed. That's your Call Stack. Inside each execution context, two phases happen: Memory Phase — Variables are stored before code runs. var gets stored as undefined. let and const? They exist but are untouchable. That zone is called the Temporal Dead Zone. Execution Phase — Now the code actually runs. Values get assigned. Functions get called. I spent 4 years writing JavaScript. Never knew this was happening under the hood. Day 2. Still here. #JavaScript #BuildingInPublic #100DaysOfCode
To view or add a comment, sign in
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗠𝘆𝘀𝘁𝗲𝗿𝘆 𝗦𝗼𝗹𝘃𝗲𝗱: 𝗧𝗵𝗲 𝗧𝗲𝗺𝗽𝗼𝗿𝗮𝗹 𝗗𝗲𝗮𝗱 𝗭𝗼𝗻𝗲 (𝗧𝗗𝗭) 🚫 Ever tried to use a variable before you've actually created it and been met with a frustrating ReferenceError? You've just met the Temporal Dead Zone, a classic JavaScript confusing point! Here's a simple breakdown of what it is and why it matters: What is it? It's the region in your code from the start of a scope until a variable is declared using let or const. During this "temporal" time, you absolutely cannot access that variable. ⏳ Why does it exist? JavaScript wants to prevent you from using uninitialized values, which can lead to buggy, unpredictable code. The TDZ is a safety net that catches these errors early. 🧱 Wait, isn't it the same as hoisting? No! Hoisting moves declarations to the top of the scope, but only let and const create this mandatory TDZ before initialization. For var, you can access it (and get undefined). The TDZ makes your code much safer and more readable. ✨ What's the best way to handle it? 𝗘𝗮𝘀𝘆: always declare your variables at the very top of their scope! This makes your intentions clear to both the engine and other developers. This is a crucial step for writing efficient, professional-grade JavaScript code. 🤩 #JavaScript #WebDevelopment #CodingTips #SoftwareEngineering #TemporalDeadZone #Hoisting #Let #Const #LearningToCode #ProgrammingConcepts
To view or add a comment, sign in
-
-
What will happen if you call a variable before initialization? 🤔 That is called Hoisting 👉 "JavaScript moves declarations to the top of their scope before execution" Sounds confusing? Let’s break it down 👇 When you create variables or functions, JavaScript runs your code in 2 phases: 1️⃣ Memory Creation Phase Before execution, JavaScript scans your code and allocates memory Example (mentally): var a → undefined let b → uninitialized (Temporal Dead Zone) 2️⃣ Execution Phase Now JavaScript runs your code line by line 👉 If you access variables before initialization: var → returns undefined let / const → ReferenceError Why does this happen? Because: var is initialized with undefined in memory let and const are hoisted but stay in the Temporal Dead Zone (TDZ) until the line where they are declared Simple way to remember: var => “exist, but don’t have a value yet” let / const => “Don’t touch before declaration” ⚡ Bonus: Function declarations are fully hoisted, so you can call them before defining them Curious how functions behave in hoisting? 🤔 Go Google function vs function expression in JavaScript — it’ll surprise you 👀 That’s hoisting in JavaScript 🚀 #javascript #webdevelopment #coding #frontend #learninpublic #hoisting
To view or add a comment, sign in
-
-
I ran a small JavaScript experiment today, and it was a good reminder that performance often hides inside simple concepts. I used the same function twice with the same inputs. The first call took noticeable time. The second call returned almost instantly. Nothing changed in the inputs. Nothing changed in the output. The only difference was that the second time, JavaScript didn’t need to do the work again. That’s the beauty of memoization. Instead of recalculating, it remembers the previous result and returns it from cache. What looks like a small optimization in code can make a big difference in how efficiently an application behaves. The deeper I go into JavaScript, the more I realize: the real power is not just in writing code — it’s in understanding how to make code smarter. #JavaScript #WebDevelopment #FrontendDevelopment #Memoization #Closures
To view or add a comment, sign in
-
-
🧠 Day 3 of 21 days challenge JavaScript Event Loop 🤯 Event Loop is a mechanism in JavaScript that handles execution of asynchronous code. It continuously checks the call stack and callback queue. If the stack is empty, it moves tasks from the queue to the stack for execution. For example :- console.log("Start"); console.log("End"); console.log("Timeout"); Wait… why this order? Because JavaScript doesn’t run everything instantly. It uses: • Call Stack • Web APIs • Callback Queue Event Loop decides what runs next. 💤For easy understanding :- Event Loop = decides execution order Sync code runs first Async code waits in queue Then runs after the stack is empty 👉 That’s why “Timeout” runs last This changed how I understand async code 🚀 #JavaScript #EventLoop #Async
To view or add a comment, sign in
-
-
Writing Cleaner Code: Mastering Template Literals in JavaScript As developers, keeping our code readable is as important as making it functional. Traditional string concatenation can often lead to messy, hard-to-maintain code. In this quick tutorial, I demonstrate how Template Strings (Backticks) can: ✅ Simplify dynamic content injection. ✅ Handle multi-line strings effortlessly. ✅ Allow logic (like Ternary Operators) directly within the string. This simple switch makes your codebase much cleaner and more professional. Check out the demo in the video! #JavaScript #WebDevelopment #SoftwareEngineering #CodingBestPractices #CleanCode
To view or add a comment, sign in
-
I've been writing JavaScript for over a year. Thought I understood var, let, and const. I didn't. "var a" inside a block accessible outside. Prints "10". "let b" inside the same block, try to access it outside and you get: ReferenceError: b is not defined Same block. Same code. Completely different behavior. Turns out var lives in global memory. let and const get their own separate block scope. Once the block is done, they're gone. This is why going back to fundamentals matters #JavaScript #WebDev #LearnInPublic #NamasteJavaScript
To view or add a comment, sign in
-
-
I used to avoid reduce method in Javascript because of how confusing it was. Today I wrote my own reduce polyfill. Handling all edge cases, it was challenging at first but then it all clicks. While writing polyfills the core flow of callback, this context and iteration is the easiest & part. But in case of reduce you need to consider following : 1. Initial values 2. Sparse arrays (holes) 3. Dynamic start indexing 4. Edge case handling with custom errors Once you work on each pointers you will understand all edge cases, reduce is not just about iteration its about the flow. #javascript
To view or add a comment, sign in
-
Day 5 of My JavaScript Journey 🚀 Today, I learned about if/else statements and type conversion in JavaScript. The if/else statement is used to control the flow of a program based on conditions. Example: if (age > 18) { console.log("Adult"); } else { console.log("Not an adult"); } I also learned about type conversion and coercion. • Type conversion is when we manually change a value from one type to another. • Type coercion is when JavaScript automatically converts types behind the scenes. For example: "5" + 2 = "52" (coercion happens) One thing that stood out to me: JavaScript can behave unexpectedly if you don’t understand type coercion. Key takeaway: Always be mindful of data types when writing conditions and operations. #JavaScript #WebDevelopment #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
📚 Blog Series Update! Between projects and busy work hours, I spent some time this week working on Part 3 of my Rediscovering JavaScript series, and now I’m happy to share a new weekend read: Variables, Scope, and Memory 🚀 In this blog post, I explore some of the most important JavaScript fundamentals: 🔹 Primitive vs reference values 🔹 Scope and scope chain 🔹 Execution context 🔹 Memory management & garbage collection These topics may sound basic, but they explain many of the “why did this happen?” moments developers face while coding. 🔗 Part 3 Rediscovering JavaScript (Part 3): Variables, Scope, and Memory: https://lnkd.in/ek8CySJc 🔗Friend link: https://lnkd.in/e4ddDhSc ✨ For this journey, I’m using Professional JavaScript for Web Developers by Nicholas C. Zakas as my main guide. ☕ Wishing you a wonderful weekend and an enjoyable read with your coffee! #JavaScript #WebDevelopment #Frontend #Programming #SoftwareEngineering #DevTips #Medium
To view or add a comment, sign in
Explore related topics
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