JavaScript doesn’t decide variable access at runtime. It decides it at write-time. That’s Lexical Scope. JavaScript looks for variables based on where the code is written, not where a function is called from. How lookup works: • Check local scope • Then parent scope • Then parent’s parent • Finally global This upward lookup is called the Scope Chain. A Lexical Environment is simply: Local memory + reference to its parent environment Important rules: • Inner functions can access outer variables • Globals are accessible (but dangerous) • Anything outside the chain = not defined JavaScript doesn’t guess. It follows the structure you wrote. If you understand this, closures, hoisting confusion, and “undefined errors” stop being mysterious. #JavaScript #LexicalScope #ScopeChain #NamasteJavaScript #LearningInPublic #FrontendDevelopment #WebDevelopment
JavaScript Lexical Scope Explained
More Relevant Posts
-
Built a real-time validation system — part of The Odin Project's JavaScript course. ✅ Email, Country, Postal Code, Password & Confirm ✅ Live inline validation: red/green feedback as you type ✅ Multi-country postal regex ✅ Password rules: letter, number, special char ✅ Accessibility: aria-invalid, aria-describedby, aria-live ✅ No libraries — just vanilla JS, HTML, CSS If all valid... you get a high five. 🌐 Live: https://lnkd.in/e9mDU_qj 💾 Code: https://lnkd.in/ercq89FN 🎥 Watch how it works #JavaScript #WebDev #FrontEnd #TheOdinProject #LearningInPublic #FormValidation
To view or add a comment, sign in
-
📌 JavaScript concat() Method – Explained Simply The concat() method in JavaScript is used to merge two or more arrays or strings and return a new combined result — without modifying the original data. 👉 Key Characteristics 🔹 Does not mutate the original array or string 🔹 Returns a new array or string 🔹 Preserves the order of elements 🔹 Can accept multiple arguments 👉 Why use concat()? 🔹 Ideal when you want to combine data safely 🔹 Helps maintain immutability, which is important in React and modern JavaScript 🔹 Makes code cleaner and more readable For arrays, concat() is often preferred over push() when you don’t want to change the original array. 🔁 Immutability leads to predictable and bug-free code. #JavaScript #WebDevelopment #Frontend #JSMethods #CleanCode #Learning
To view or add a comment, sign in
-
-
I recently stumbled on a neat JavaScript quirk: an object can actually hide a method from its prototype just by having a property with the same name. Even then, the method itself isn’t lost! You can still call it by borrowing it from Object.prototype and explicitly setting this. It’s a small reminder of how objects in JS separate data from behavior, and how flexible prototypes really are. #JavaScript #WebDev #CodingTips #DeveloperLife #JSQuirks
To view or add a comment, sign in
-
-
JavaScript's got a thing or two to teach us about variables. It's pretty straightforward, actually - everything's passed by value. But, what's that even mean? It means when you pass a primitive, like a number or a string, JavaScript just makes a copy of it. Simple as that. The original and the copy, they're like two separate entities, living their best lives independently. So, yeah: Primitives are stored right in the variable, no fuss. But objects, on the other hand, are a different story - they're passed by value too, but the value is actually a reference to where the object lives in memory. Think of it like sending someone a map to your house, instead of the actual house. You can make copies of objects, though, using the spread operator or structuredClone() - it's like taking a snapshot of the object, so you can play around with the copy without messing up the original. Here's the lowdown: Primitives are safe, they won't get changed on you. Objects and arrays, though, they share references, so be careful not to mess things up. Just use spread or structuredClone() to make copies, and you're golden. And, honestly, embracing immutability is the way to go - it makes your code predictable, and performant, like a well-oiled machine. Check it out: https://lnkd.in/g-Nj9Rh6 #JavaScript #Variables #Immutability
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
-
✨ Understanding var, let, and const in JavaScript ✨ One of the first hurdles for beginners in JavaScript is figuring out when to use var, let, or const. While they all declare variables, the differences matter for clean, bug-free code: 🔹var – Function-scoped, allows redeclaration, and can lead to unexpected behavior due to hoisting. Best avoided in modern code. 🔹 let – Block-scoped, can be updated but not redeclared in the same scope. Ideal for variables whose values change over time. 🔹 const – Block-scoped, must be initialized at declaration, and cannot be reassigned. Perfect for constants or values that should remain fixed. Mastering these keywords is a small step that makes a big difference in writing clean, predictable, and modern JavaScript🚀 #JavaScript #WebDevelopment #FrontendDevelopment #CodingTips #LearnToCode #CleanCode #DeveloperLife #TechCommunity
To view or add a comment, sign in
-
JavaScript: Fetch API, Promises & Async/Await 🌐⚙️ These concepts are must-know for handling API calls and async operations in modern JavaScript. 🔹 Fetch API Used to make HTTP requests. It returns a Promise. 🔹 Promises Handle async results with 3 states: pending → resolved → rejected 🔹 Async/Await A cleaner and more readable way to work with Promises using try/catch. 🧠 Practice Tip: ✔ Fetch data from a public API ✔ Rewrite it using async/await ✔ Handle errors properly Save this if you’re learning JavaScript 🚀 #JavaScript #WebDevelopment #AsyncJavaScript #FetchAPI #FrontendDevelopment #LearnToCode #DevTips
To view or add a comment, sign in
-
-
`𝗳𝗼𝗿𝗘𝗮𝗰𝗵` 𝗹𝗼𝗼𝗸𝘀 𝗶𝗻𝗻𝗼𝗰𝗲𝗻𝘁. 𝗨𝗻𝘁𝗶𝗹 𝘆𝗼𝘂 𝗺𝗶𝘅 𝗶𝘁 𝘄𝗶𝘁𝗵 𝗮𝘀𝘆𝗻𝗰 𝗰𝗼𝗱𝗲. Many async bugs don’t throw errors. They just quietly do the wrong thing. `forEach` doesn’t wait. It doesn’t respect `await`. So your logic runs out of order while your code 𝗹𝗼𝗼𝗸𝘀 correct. This is one of those traps that separates “working code” from 𝗿𝗲𝗹𝗶𝗮𝗯𝗹𝗲 𝗰𝗼𝗱𝗲. If you write async JavaScript, this is worth a pause. 𝗥𝗲𝗮𝗱 𝗺𝗼𝗿𝗲: https://lnkd.in/dww_BYP2 #JavaScript #AsyncProgramming #CleanCode #WebDevelopment #DeveloperMindset
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
-
-
🛑 Read Twice Before Answering , JavaScript Thinks Faster Than We Do This looks harmless. But many answers go wrong because of how JavaScript evaluates parameters 👀 function test(a = 10, b = a) { console.log(a, b); } test(5); No tricks. No loops. No async. Still… people pause here 😄 🤔 Why this question is interesting Tests default parameters Tests evaluation order Looks beginner-friendly Still makes seniors think twice Very common interview logic 💬 Your Turn Comment your answer like this 👇 a → b → ⚠️ Please don’t run the code. Answer based on understanding. I will post the correct output + simple explanation in the evening. 📌 This post is to understand JavaScript behavior, not production patterns. #JavaScript #LearnJS #FrontendDevelopment #CodingInterview #TechWithVeera #WebDevelopment
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