𝐓𝐞𝐦𝐩𝐨𝐫𝐚𝐥 𝐃𝐞𝐚𝐝 𝐙𝐨𝐧𝐞 𝐞𝐱𝐩𝐥𝐚𝐢𝐧𝐬 𝐰𝐡𝐲 𝐥𝐞𝐭 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐭 𝐝𝐨𝐧’𝐭 𝐛𝐞𝐡𝐚𝐯𝐞 𝐥𝐢𝐤𝐞 𝐯𝐚𝐫. For a long time, hoisting felt inconsistent to me. Variables seemed to exist before execution, yet accessing some of them caused errors while others didn’t. The confusion cleared once I stopped thinking only in terms of hoisting and started looking at 𝐦𝐞𝐦𝐨𝐫𝐲 𝐚𝐥𝐥𝐨𝐜𝐚𝐭𝐢𝐨𝐧. When JavaScript begins execution, it allocates memory before running any line of code. For var, this memory is allocated in the 𝐠𝐥𝐨𝐛𝐚𝐥 𝐬𝐜𝐨𝐩𝐞 and initialized with undefined. For let and const, memory is allocated in a 𝐬𝐞𝐩𝐚𝐫𝐚𝐭𝐞 𝐬𝐜𝐫𝐢𝐩𝐭 𝐦𝐞𝐦𝐨𝐫𝐲 𝐬𝐩𝐚𝐜𝐞, not the global one. This memory exists , but it’s 𝐧𝐨𝐭 𝐚𝐜𝐜𝐞𝐬𝐬𝐢𝐛𝐥𝐞 until initialization. That gap is the 𝐓𝐞𝐦𝐩𝐨𝐫𝐚𝐥 𝐃𝐞𝐚𝐝 𝐙𝐨𝐧𝐞 (𝐓𝐃𝐙). What helped this click: • TDZ is the time between 𝐡𝐨𝐢𝐬𝐭𝐢𝐧𝐠 𝐚𝐧𝐝 𝐢𝐧𝐢𝐭𝐢𝐚𝐥𝐢𝐳𝐚𝐭𝐢𝐨𝐧 • Accessing a variable inside TDZ throws a 𝐑𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞𝐄𝐫𝐫𝐨𝐫 • TDZ prevents usage before a value is safely assigned • It’s different from 𝐒𝐲𝐧𝐭𝐚𝐱𝐄𝐫𝐫𝐨𝐫 and 𝐓𝐲𝐩𝐞𝐄𝐫𝐫𝐨𝐫 What changed my approach was realizing TDZ isn’t something to avoid — it’s something to design around. By placing declarations and initializations at the top of the scope, we effectively 𝐬𝐡𝐫𝐢𝐧𝐤 𝐭𝐡𝐞 𝐓𝐃𝐙 𝐰𝐢𝐧𝐝𝐨𝐰 𝐭𝐨 𝐳𝐞𝐫𝐨. Once initialization happens first, execution becomes predictable — and unexpected errors disappear. #JavaScript #SoftwareEngineering #ProblemSolving #DeveloperJourney #LearningInPublic #TechCommunity #ContinuousLearning
Understanding Temporal Dead Zone (TDZ) in JavaScript
More Relevant Posts
-
𝗪𝗲𝗹𝗰𝗼𝗺𝗲 𝘁𝗼 𝗗𝗮𝘆 𝟮𝟬 What if you could control every interaction with an object — reads, writes, deletes — without changing the object itself? That’s what Proxy does. 𝘼 𝙋𝙧𝙤𝙭𝙮 𝙞𝙨 𝙖 𝙬𝙧𝙖𝙥𝙥𝙚𝙧 𝙖𝙧𝙤𝙪𝙣𝙙 𝙖𝙣 𝙤𝙗𝙟𝙚𝙘𝙩 𝙩𝙝𝙖𝙩 𝙞𝙣𝙩𝙚𝙧𝙘𝙚𝙥𝙩𝙨 𝙡𝙤𝙬-𝙡𝙚𝙫𝙚𝙡 𝙤𝙥𝙚𝙧𝙖𝙩𝙞𝙤𝙣𝙨 𝙡𝙞𝙠𝙚: -> Property access -> Property assignment -> Property deletion -> Function calls -> Object construction 𝙄𝙣𝙨𝙩𝙚𝙖𝙙 𝙤𝙛: Engine → Object 𝙄𝙩 𝙗𝙚𝙘𝙤𝙢𝙚𝙨: Engine → Proxy → Object The Proxy decides what happens. 𝙒𝙝𝙮 𝙄𝙩 𝙈𝙖𝙩𝙩𝙚𝙧𝙨: 𝙋𝙧𝙤𝙭𝙮 𝙡𝙚𝙩𝙨 𝙮𝙤𝙪 𝙘𝙤𝙣𝙩𝙧𝙤𝙡 𝙗𝙚𝙝𝙖𝙫𝙞𝙤𝙧, 𝙣𝙤𝙩 𝙟𝙪𝙨𝙩 𝙙𝙖𝙩𝙖. You can: -> Validate values before saving -> Block unknown properties -> Log every change -> Create computed/virtual properties 𝘛𝘩𝘦 𝘰𝘣𝘫𝘦𝘤𝘵 𝘴𝘵𝘢𝘺𝘴 𝘵𝘩𝘦 𝘴𝘢𝘮𝘦. 𝘛𝘩𝘦 𝘪𝘯𝘵𝘦𝘳𝘢𝘤𝘵𝘪𝘰𝘯 𝘳𝘶𝘭𝘦𝘴 𝘤𝘩𝘢𝘯𝘨𝘦. #JavaScript #JSInternals #FrontendEngineering #WebDevelopment #SoftwareEngineering #ECMAScript #MetaProgramming #FrontendArchitecture #TechDeepDive #Coding
To view or add a comment, sign in
-
-
Day 17 of #100DaysOfCode: Refactoring & The Art of "Unpacking" 📦 Today was a refactoring day. I took my Shopping List code from earlier this week and cleaned it up using modern JavaScript patterns. Two big changes I forced myself to make: Destructuring: Instead of writing item.name and item.price ten times, I learned to unpack them directly in the function arguments: const calculate = ({ price, qty }) => ... It cuts the noise and makes the code so much more readable. No forEach for logic: I set a strict rule: forEach is ONLY for printing to the console. Need a new array? Use map. Need a subset? Use filter. Need a total? Use reduce. DSA Update: Didn't get to DSA today. I got lost in the refactoring process, but getting the syntax right feels like a win. #JavaScript #WebDevelopment #CleanCode #100DaysOfCode #Refactoring
To view or add a comment, sign in
-
It's 5:45 am & I am still looking at my dreams. Today[2/28/2026] is day 23 of learning javascript Today & tommorow i will learn are: 1. Difference between let, const & var 2. How to use the default parameter 3. Template string, Multiline string, Dynamic string 4. Arrow Function Syntax, params 5. Spread Operator, Array Max, Copy Arrays 6. Object & Array destructuring 7. Keys, Values, Entries, Delete, Seal, Freeze 8. Accessing Object Data: Nested Object, Optional Chaining 9. Looping Object 10. Primitive Type, Non Primitive Type 11. Null Vs Undefines 12. Truthy & Falsy Values 13. ==, === , implicit conversion 14. Block Scope, Global Scope, Simple Unders. of Hoisting 15. Closure 16. Callback Function & pass different function 17. Function Arguments, pass by ref. pass by value 18. Map, ForEach 19. Filter, Find, Reduce #letsconnect #programmer #frontenddeveloper #mernstakedeveloper #Coding
To view or add a comment, sign in
-
𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝘃𝘀 𝗠𝗲𝘁𝗵𝗼𝗱𝘀 — 𝘄𝗵𝗮𝘁’𝘀 𝘁𝗵𝗲 𝗿𝗲𝗮𝗹 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲? A 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 is a standalone block of logic. It exists independently and doesn’t belong to any object. A 𝗺𝗲𝘁𝗵𝗼𝗱 is a function that belongs to an object or class. It operates on the data owned by that object. So, can we call a method a function? Technically, yes. Conceptually, no. Under the hood, a method is a function. But once a function is attached to an object, we call it a method to describe its role. Example in JavaScript: parseInt() → function array.map() → method (a function bound to an array) Calling everything a function isn’t wrong, but it hides intent. Using the right term makes your design clearer and your code easier to reason about. #Programming #JavaScript #WebDevelopment #SoftwareEngineering #CodingConcepts #ComputerScience #CleanCode #CodeClarity #DevTips #LearningToCode #MERN #FrontendDevelopment #BackendDevelopment
To view or add a comment, sign in
-
-
🚀 LeetCode: Subarray Sum The goal is to find a contiguous subarray that adds up to a specific target sum and return its 1-indexed positions. This is a classic problem that tests your ability to manage a dynamic range within an array. 🛠️ My Approach 1. Sliding Window Initialization: I initialized two pointers, s (start) and e (end), both starting at the beginning of the array to represent a dynamic window. 🔡 2. Window Expansion: I used a for loop to move the end pointer e forward, adding each element to a running sum to expand the window. 🧹 3. Dynamic Shrinking: If the current sum exceeded the target, I used a while loop to move the start pointer s forward, subtracting elements from the sum until it was no longer greater than the target. 📍 ❌ If the sum exactly matches the target, we return the 1-indexed positions [s + 1, e + 1] immediately. 📊 Efficiency Analysis ⏱️ Time Complexity: O(n) 💾 Space Complexity: O(1) #LeetCode #JavaScript #CodingLife #Algorithms #WebDevelopment #ProblemSolving #SoftwareEngineering #TechCommunity
To view or add a comment, sign in
-
-
𝐀𝐭 𝐟𝐢𝐫𝐬𝐭, { } 𝐥𝐨𝐨𝐤𝐞𝐝 𝐥𝐢𝐤𝐞 𝐣𝐮𝐬𝐭 𝐬𝐲𝐧𝐭𝐚𝐱. Then I realized it actually defines 𝐡𝐨𝐰 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐜𝐨𝐧𝐭𝐫𝐨𝐥𝐬 𝐚𝐜𝐜𝐞𝐬𝐬. The confusion started when variables behaved differently inside and outside blocks. Some were accessible, some weren’t — even though they were declared only a few lines apart. That’s when 𝐛𝐥𝐨𝐜𝐤 𝐬𝐜𝐨𝐩𝐞 and 𝐬𝐡𝐚𝐝𝐨𝐰𝐢𝐧𝐠 started to make sense. In JavaScript, a 𝐛𝐥𝐨𝐜𝐤 (also called a 𝐜𝐨𝐦𝐩𝐨𝐮𝐧𝐝 𝐬𝐭𝐚𝐭𝐞𝐦𝐞𝐧𝐭) is a group of statements wrapped in { }. Blocks exist so multiple statements can be used where JavaScript expects a single one — like in if, for, or while. This grouping directly affects scope. What clarified it for me: A 𝐛𝐥𝐨𝐜𝐤 𝐬𝐜𝐨𝐩𝐞 allows access only within its { }. let and const are 𝐡𝐨𝐢𝐬𝐭𝐞𝐝 𝐢𝐧𝐬𝐢𝐝𝐞 𝐭𝐡𝐞 𝐛𝐥𝐨𝐜𝐤 𝐬𝐜𝐨𝐩𝐞. They get 𝐬𝐞𝐩𝐚𝐫𝐚𝐭𝐞 𝐦𝐞𝐦𝐨𝐫𝐲 𝐬𝐩𝐚𝐜𝐞 limited to that block. var is hoisted to the 𝐠𝐥𝐨𝐛𝐚𝐥 (𝐨𝐫 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧) 𝐬𝐜𝐨𝐩𝐞. After the block ends, let and const are inaccessible, but var is not. 𝐒𝐡𝐚𝐝𝐨𝐰𝐢𝐧𝐠 𝐡𝐚𝐩𝐩𝐞𝐧𝐬 𝐰𝐡𝐞𝐧 𝐚 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞 𝐢𝐧𝐬𝐢𝐝𝐞 𝐚 𝐛𝐥𝐨𝐜𝐤 𝐨𝐯𝐞𝐫𝐫𝐢𝐝𝐞𝐬 𝐚 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞 𝐰𝐢𝐭𝐡 𝐭𝐡𝐞 𝐬𝐚𝐦𝐞 𝐧𝐚𝐦𝐞 𝐨𝐮𝐭𝐬𝐢𝐝𝐞 𝐢𝐭. With let and const, this is intentional and scoped. With var, things get risky. 𝐈𝐥𝐥𝐞𝐠𝐚𝐥 𝐬𝐡𝐚𝐝𝐨𝐰𝐢𝐧𝐠 occurs when a let variable is shadowed by var, because var ignores block boundaries. What changed my thinking is realizing that block scope still follows 𝐥𝐞𝐱𝐢𝐜𝐚𝐥 𝐬𝐜𝐨𝐩𝐢𝐧𝐠 . Scopes are determined by where the code is written — not how it runs. Once that clicks, shadowing stops being mysterious and starts being predictable. #JavaScript #SoftwareEngineering #ProblemSolving #DeveloperJourney #LearningInPublic #TechCommunity #ContinuousLearning
To view or add a comment, sign in
-
-
I tried implementing the internal working of forEach and map to better understand how they operate under the hood. Both are higher order functions because they accept another function as an argument. Internally, they iterate over the array (typically via a loop) and execute the callback for each element. forEach : Iterates through the array. Executes the provided callback on each element. Does not return a new array (returns undefined). Primarily used for logging map : Iterates through the array. Executes the callback on each element. Stores the callback’s return value in a new array. Returns the new transformed array. Does not mutate the original array. Rewriting these methods manually with a for loop really clarifies their behavioral difference : forEach is about execution, map is about transformation. #JavaScript #WebDevelopment #FunctionalProgramming #LearnInPublic #ChaiAurCode
To view or add a comment, sign in
-
-
Day 184: Moving Nodes and Counting Words I am now on Day 184! Today, I practiced more Linked List logic and started working with Strings. Here is what I did today in very simple steps: 1. Rotate List (LeetCode 61) 🔄 I learned how to take the end of a list and move it to the front. How? First, I find the length of the list. Then, I find the right place to "cut" the list and connect the end back to the start. 2. Swap Nodes in Pairs (LeetCode 24) 🤝 I learned how to swap every two nodes. For example, 1 -> 2 -> 3 -> 4 becomes 2 -> 1 -> 4 -> 3. The Trick: I used a "Sentinel" node to keep track of the head. I also tried a Recursive version, which made the code very short and clean! 3. Length of Last Word (LeetCode 58) 📏 I moved to Strings! I had to find the length of the very last word in a sentence. Manual Way: Instead of using easy shortcuts, I started from the end of the string, skipped the empty spaces, and counted the letters until I hit another space. It is a great way to practice loops. My takeaway: Whether it is cutting a list or counting letters backward, logic is all about finding the right starting point! #JavaScript #Coding #Programming #WebDevelopment #DataStructures #Algorithms #SoftwareEngineer #Logic #SimpleLearning #StringManipulation #LinkedList #TechCommunity #DailyCoding #ProblemSolving #CareerGrowth #CodeNewbie
To view or add a comment, sign in
-
🚀 Codewars Kata Reflection: “What’s the Real Floor?” (8 kyu, JavaScript) Today I revisited a deceptively simple Codewars challenge that reinforced an important lesson: clarity in problem modeling beats complexity in code. 🧠 The problem American and European floor numbering systems differ: In the US, the 1st floor is actually the ground floor There is no 13th floor Basements (negative floors) remain unchanged The task was to convert an American floor number into its European equivalent. 💡 Why the solution works function getRealFloor(n) { return n <= 0 ? n : n < 13 ? n - 1 : n - 2; } This works because it mirrors the real-world rules directly: Basements (n <= 0) These are universal - no transformation needed. Floors 1-12 Since “1” maps to ground floor, all values shift down by 1. Floors above 13 Two numbers are missing below them: Floor 1 (ground floor) Floor 13 So we shift down by 2. Instead of looping or mapping values, the logic is handled with simple conditional branching, making the intent obvious and the behavior predictable. ⚡ Time & Space Complexity Time Complexity: O(1) - constant time, no loops, no recursion Space Complexity: O(1) - no additional memory allocated. 📚 What I learned 1. Not every problem needs iteration - sometimes rules are enough 2. Writing code that reflects real-world constraints leads to simpler solutions 3. Even beginner-level katas are great for sharpening logic, edge-case thinking, and code expressiveness. Small challenges, big reminders. On to the next kata 🧩💪 #Codewars #JavaScript #ProblemSolving #CleanCode #SoftwareEngineering #LearningInPublic #Algorithms
To view or add a comment, sign in
-
-
Day 26 of 30-day Coding Sprint 992. Subarrays with K Different Integers - The Problem: Count every single contiguous subarray that contains exactly k different integers. - The Challenge: A standard sliding window is "greedy"; it finds the largest or smallest window that fits a condition. But here, multiple windows of different sizes ending at the same index r could all have exactly k integers. Approach 1: Brute Force - Generating all subarrays and checking unique counts using a HashMap. - Complexity: O(n^2). This will TLE (Time Limit Exceeded) on any competitive platform with a large input. Approach 2: The "Exactly K" via "At Most K" Logic (Optimal) - The Strategy: Just like we did with binary sums on Day 21, we use the formula: Exactly(K) = AtMost(K) - AtMost(K-1) - The Helper: helper(nums, k) counts how many subarrays have at most $k$ distinct elements. - Why it works: Finding "at most" is easy with a sliding window: if map.size > k, we shrink from the left. The number of subarrays ending at r that satisfy "at most k" is simply (r - l + 1). - Complexity: O(n) time and O(k) space. #30DaysOfCode #DSASprint #LeetCode #JavaScript #SlidingWindow #HardProblem #Algorithms #Consistency
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