Day 47/100 – Understanding JavaScript Scope (Global vs Local) 🧠 Today I spent time understanding one of the most important JavaScript concepts: scope. Scope defines where a variable can be accessed in your code. At first, this topic felt confusing. But once I slowed down and practiced, it started to make sense. There are mainly two types of scope I focused on: 🔹 Global Scope Variables declared outside any function. They can be accessed anywhere in the program. 🔹 Local (Function) Scope Variables declared inside a function. They can only be used inside that function. Why this matters so much: ✔️ Helps avoid unexpected errors ✔️ Prevents variable name conflicts ✔️ Makes code more predictable ✔️ Improves readability and maintenance One big lesson: Just because code works doesn’t mean it’s written well. Good code is: Readable. Predictable. Easy to understand. I’m learning that becoming a better developer isn’t about memorizing syntax. It’s about understanding how things work behind the scenes. Still learning. Still practicing. Still showing up. Day 47 complete ✅ On to Day 48 🚀 #100DaysOfCode #JavaScript #LearningInPublic #WebDevelopment #FrontendDevelopment #CodingJourney #Consistency
Understanding JavaScript Scope: Global vs Local Variables
More Relevant Posts
-
You’ve probably seen this error many times in JavaScript: “SyntaxError: Unexpected token...” It’s so common that most of us just fix the typo and move on. But have you ever wondered why the error specifically mentions a “𝙩𝙤𝙠𝙚𝙣”? Here’s what’s really happening under the hood: Before JavaScript executes your code, the engine first splits the source into small pieces called tokens - keywords, identifiers, numbers, operators, brackets, and punctuation. Next, those tokens are parsed into a structured representation called an AST (Abstract Syntax Tree). This tree is what the JS engine actually uses to understand and run your program. For example, even a simple line like: 𝗹𝗲𝘁 𝗮 = 𝟭𝟬; becomes a full tree structure in the parser (you can see this in the attached AST). If any token appears where the grammar doesn’t allow it, the tree cannot be formed. And that’s the exact moment JavaScript stops and reports: 𝘜𝘯𝘦𝘹𝘱𝘦𝘤𝘵𝘦𝘥 𝘵𝘰𝘬𝘦𝘯. So a small missing bracket or extra comma isn’t just a typo - it’s the parser failing to construct a valid program from the token stream. #JavaScript #WebDevelopment #Programming #Debugging #SoftwareEngineering #JSInternals
To view or add a comment, sign in
-
-
🔗 What is Promise Chaining in JavaScript? When working with asynchronous operations in JavaScript, we often need to perform tasks in sequence — one after another. This is where Promise Chaining comes in. Promise chaining means attaching multiple .then() methods to a promise so that the output of one step becomes the input of the next. ✅ Why It’s Needed : Imagine: 🔹 Fetching user data 🔹 Then fetching that user’s posts 🔹 Then processing those posts Each step depends on the previous one. Promise chaining allows us to handle this flow cleanly and predictably. 👉 How It Works : Each .then(): 🔹 Waits for the previous promise to resolve 🔹 Receives its result 🔹 Returns a new promise 🔹 That returned promise is passed to the next .then() in the chain. Example: 🔹 Each step runs only after the previous one completes 🔹 Errors are handled in a single .catch() 🚀 Benefits of Promise Chaining ✔ Avoids “callback hell” ✔ Keeps asynchronous code readable ✔ Maintains clean execution flow ✔ Centralized error handling 💡 Key Insight 👉 If you return a value → it automatically becomes a resolved promise. 👉 If you return a promise → the next .then() waits for it. #JavaScript #WebDevelopment #Frontend #AsyncProgramming #Promises
To view or add a comment, sign in
-
-
MAYBE IT’S TIME WE TALK ABOUT ONE OF THE MOST MISUNDERSTOOD KEYWORD IN JAVASCRIPT — this. You think you understand it… until it breaks in production. In OOP-style classes, 'this' is NOT determined by where a function is written. It is determined by HOW it is called. That’s why your class method suddenly becomes UNDEFINED when passed as a callback. Common headaches: - Losing context inside event handlers - setTimeout destroying your method binding - Writing .bind(this) everywhere - The old const self = this workaround Then ARROW FUNCTIONS came in. Arrow functions DO NOT have their own this. They inherit this from the surrounding scope. Result: - No manual binding - Cleaner class code - Less mental overhead - Fewer unexpected bugs But remember: - Arrow functions are not constructors - They don’t replace understanding execution context - They solve binding issues, not bad architecture REAL JAVASCRIPT MASTERY starts when you truly understand THIS — not when you memorize syntax. What was your most confusing THIS bug? #JavaScript #WebDevelopment #NodeJS #Frontend #Programming
To view or add a comment, sign in
-
-
💡 Understanding the JavaScript Event Loop (Made Simple) When I started learning JavaScript, I was confused about how setTimeout, button clicks, and API calls worked — especially since JavaScript is single-threaded. This visual really helped me understand what happens behind the scenes: 👉 1. Call Stack – Runs code line by line (synchronous code) 👉 2. Web APIs – Handles async tasks like timers and fetch requests 👉 3. Callback Queue – Stores completed async callbacks 👉 4. Event Loop – Moves callbacks to the stack when it’s empty 🔎 Simple Example: console.log("Start"); setTimeout(() => { console.log("Inside Timeout"); }, 0); console.log("End"); 👉 What do you think the output will be? The output is: Start End Inside Timeout Even though the timeout is set to 0 milliseconds, it doesn’t run immediately. Here’s why: 1️⃣ "Start" goes to the call stack → executes 2️⃣ setTimeout moves to Web APIs 3️⃣ "End" executes 4️⃣ The callback moves to the queue 5️⃣ The Event Loop waits until the stack is empty 6️⃣ Then it pushes "Inside Timeout" to the stack That’s the Event Loop in action 🚀 Understanding this concept made: ✅ Promises easier ✅ Async/Await clearer ✅ Debugging smoother If you're learning JavaScript, mastering the Event Loop is a big step forward. #JavaScript #WebDevelopment #BeginnerDeveloper #AsyncProgramming #FrontendDevelopment #mernstack #fullstack
To view or add a comment, sign in
-
-
Ever wondered what actually happens behind the scenes when JavaScript runs your code? 🤔 How Variables & Functions Work Behind the Scenes in JavaScript Today I focused on understanding what actually happens inside the JavaScript engine when we write variables and functions — and it completely changed how I read JS code. 🧠 Step 1: JavaScript Creates an Execution Context Before executing any code, JavaScript creates an Execution Context. This happens in two phases: 1. Creation Phase (Memory Allocation Phase) In this phase, JavaScript scans the entire code before running it and prepares memory. ✔ Variables var variables are allocated memory and initialized with undefined let and const are also allocated memory, but they stay in the Temporal Dead Zone (TDZ) until initialized ✔ Functions Function declarations are stored fully in memory (function body included) Function expressions & arrow functions behave like variables and depend on var / let / const 👉 This is the real reason hoisting exists. 2.Execution Phase (Code Runs Line by Line) Now JavaScript starts executing the code: Variables get their actual values Functions are executed when they are called A new Function Execution Context is created for every function call Each context is pushed to the Call Stack After execution, it is removed from the stack Why Functions Can Be Called Before Declaration? because function declarations are fully hoisted during the creation phase. 💡 Key Takeaways JavaScript doesn’t execute code directly — it prepares first Hoisting is a byproduct of the creation phase var, let, and const differ because of how memory is allocated Understanding execution context makes debugging much easier. Mastering the basics is what truly levels up a developer. Thanks to Anshu Pandey and Sheryians Coding School #JavaScript #JavaScriptbasics #ES6 #JSEngine #coding
To view or add a comment, sign in
-
-
𝗪𝗵𝗲𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗗𝗲𝗰𝗶𝗱𝗲𝘀 𝗠𝗮𝘁𝗵 𝗜𝘀 𝗮 𝗦𝘂𝗴𝗴𝗲𝘀𝘁𝗶𝗼𝗻 2 + 0 = 20 6 + 6 = 66 𝗪𝗿𝗼𝗻𝗴? Not if you speak JavaScript. 𝗘𝘃𝗲𝗿𝘆 𝗝𝗦 𝗱𝗲𝘃’𝘀 𝗳𝗶𝗿𝘀𝘁 𝗿𝗲𝗮𝗰𝘁𝗶𝗼𝗻: “Hold on… what’s the type?” 👀 Because in JavaScript: "2" + 0 becomes "20" "6" + "6" becomes "66" The + operator multitasks Your assumptions don’t 🧠 JavaScript doesn’t guess intent. 𝗜𝘁 𝗳𝗼𝗹𝗹𝗼𝘄𝘀 𝗿𝘂𝗹𝗲𝘀: 𝗳𝗹𝗲𝘅𝗶𝗯𝗹𝗲, 𝗱𝗮𝗻𝗴𝗲𝗿𝗼𝘂𝘀 𝗿𝘂𝗹𝗲𝘀. 𝗪𝗵𝗮𝘁 𝗹𝗼𝗼𝗸𝘀 𝗹𝗶𝗸𝗲 𝗯𝗿𝗼𝗸𝗲𝗻 𝗺𝗮𝘁𝗵 𝗶𝘀 𝗮𝗰𝘁𝘂𝗮𝗹𝗹𝘆: 👉 implicit type coercion 👉 string concatenation 👉 logic doing exactly what you asked for 😅 𝗧𝗵𝗲 𝗿𝗲𝗮𝗹 𝗯𝘂𝗴 𝗶𝘀𝗻’𝘁 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁. It’s forgetting that inputs lie. 📌 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 𝗳𝗼𝗿 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿𝘀: • Be explicit with types • Validate inputs early • Never assume + means addition JavaScript gives you freedom. And freedom, as usual, comes with consequences. 🤝 𝗘𝘃𝗲𝗿𝘆 𝗝𝗦 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗹𝗲𝗮𝗿𝗻𝘀 𝘁𝗵𝗶𝘀 𝘁𝗵𝗲 𝗵𝗮𝗿𝗱 𝘄𝗮𝘆 𝗼𝗻𝗰𝗲. #JavaScript #TypeCoercion #FrontendDevelopment #ProgrammingHumor #CodingTips
To view or add a comment, sign in
-
-
1️⃣ Most messy JavaScript code I review isn’t wrong — it’s just hard to read. 2️⃣ One of the easiest ways to spot beginner JavaScript code is string concatenation everywhere. 3️⃣ Clean code rarely comes from big rewrites — it comes from small syntax choices. 4️⃣ When readability improves, bugs usually drop. This is one small example. 5️⃣ I can often tell a developer’s experience level by how they build strings in JavaScript. 6️⃣ If you mentor junior JavaScript devs, you’ve seen this pattern a lot. 7️⃣ Code reviews show the same small mistake over and over — and it’s easy to fix. 8️⃣ A 10-second syntax change can noticeably improve code clarity. 9️⃣ Modern JavaScript already solved this problem — many developers still don’t use it. 🔟 ES6 shipped years ago, but some of its best readability features are still underused. I shared a quick breakdown in this short video. Follow for more practical JS patterns. #javascript #softwaredeveloper #learn #video
To view or add a comment, sign in
-
🚀 How JavaScript Actually Executes Code (And Why It Matters) Spent time breaking down JavaScript internals to improve debugging and async understanding. Here’s the mental model that changed everything 👇 🧠 Core Concepts: • Execution Context (Creation → Execution) • Call Stack (LIFO execution flow) • Lexical Scope & Scope Chain • Hoisting (var vs let behavior) • Closures (reference-based memory) • Event Loop (Microtasks > Macrotasks priority) 💡 Key Insight: JavaScript isn’t unpredictable — it follows a strict execution flow: Execution Context → Call Stack → Event Loop Understanding this made async code, promises, and debugging much more intuitive. 🧩 Problem Solving (Pattern-Oriented Approach) Worked on: • Two Sum → HashMap pattern (O(n)) • Best Time to Buy/Sell Stock → Greedy (min tracking) • Move Zeroes → Two Pointers (in-place) • Contains Duplicate → Hashing / Set Instead of jumping into code, focused on identifying patterns first. 💡 Takeaway: Strong problem-solving comes from recognizing patterns — not memorizing solutions. Building stronger fundamentals to write better, more predictable code. 💪 #JavaScript #DSA #WebDevelopment #ProblemSolving #FrontendDeveloper #MERNStack Visualizing how JavaScript executes code helped everything click 👇
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