🚨 Still using var in JavaScript without knowing the difference? This might be breaking your code… 💡 Understanding var, let, and const is one of the first steps to writing clean and predictable JavaScript. 🔹 var • Function scoped • Can be re-declared and re-assigned • Gets hoisted with undefined • Often leads to unexpected bugs in modern code 🔹 let • Block scoped (safer than var) • Can be re-assigned but not re-declared in the same scope • Helps avoid scope-related bugs 🔹 const • Block scoped • Cannot be re-assigned after declaration • Must be initialized at declaration • Perfect for values that shouldn't change ⚡ Quick rule many developers follow: • Use const by default • Use let when a value needs to change • Avoid var in modern JavaScript 📌 Small concept. Big impact on code quality. #JavaScript #WebDevelopment #Coding #FrontendDevelopment #100DaysOfCode #LearnToCode
JavaScript var vs let vs const: Understanding Scope and Hoisting
More Relevant Posts
-
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
-
-
Today I focused on understanding loops and functions in JavaScript and it really changed how I think about writing code. I learned how to use: for loops, while loops,how iteration works step by step, function return,callback function,scope. At first, it was a bit confusing (especially getting stuck in an infinite loop 😅) but after practicing, I started seeing how powerful loops are for repeating tasks and reducing code. One thing that stood out to me is how a small mistake (like using the wrong increment/decrement) can completely break your logic. Then I moved into functions which made everything cleaner and more organized. Instead of repeating the same code, I can now wrap logic inside a function and reuse it anytime. #JavaScript #FrontendDevelopment #LearningToCode
To view or add a comment, sign in
-
-
Day 1 🧠 Understanding Lexical Scoping in JavaScript (in 2 minutes) One concept that quietly powers a lot of JavaScript behavior is lexical scoping. 👉 Simply put: A function remembers where it was written, not where it is called. 🔍 Example: let name = "Global"; function print() { console.log(name); } function test() { let name = "Local"; print(); } test(); // Output: Global 💡 Even though print() is called inside test(), it still logs "Global". Why? Because print() was defined in the global scope, so it uses that scope. 🧠 Key Takeaways: Scope is determined at write time (lexical), not run time. JavaScript looks for variables in the scope chain upward. This is the foundation of closures. 🚀 Why this matters: Understanding lexical scoping helps you: ✔ Write predictable code ✔ Debug faster ✔ Master closures, callbacks, and async logic ✔ Work better with React hooks 🔥 One-line takeaway: 👉 "Where you write your function decides what it can access." If you're learning JavaScript fundamentals, don’t skip this — it shows up everywhere. #JavaScript #WebDevelopment #Frontend #Coding #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
-
-
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
-
Day 1 of 30 days of javascript challenge. problem-2667 Problem - Write a function createHelloWorld that returns another function, that returns "Hello World" As this is my first code, I revised my notes on javascript scope. ☑️ Function scope - Any variables declared inside a function body cannot be accessed outside the function body, but global variables can be used inside function body ☑️ Block scope - Any variable declared inside { } cannot be used outside the { } block, although it supports only let and const keyword, var can be used ☑️ Lexical scope - A variable declared outside a function can be accessed inside another function defined after the variable declaration. (The opposite is not true ) This problem uses the concept of closures and higher order functions. Please feel free to discuss where can I improve the code or if you have a different perspective, comment below your views. #javascript #coding #development #motivation #goals #leetcode #webdevelopment
To view or add a comment, sign in
-
-
In JavaScript, the super keyword plays a crucial role in class inheritance. It allows child classes to access and reuse logic from parent classes, whether it’s constructors or methods. Using super() ensures proper initialization, while super.method() enables clean code reuse. Understanding this concept is key to writing scalable and maintainable object-oriented JavaScript.
To view or add a comment, sign in
-
-
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 wondered how JavaScript keeps track of which function is running? JavaScript uses something called the Call Stack. Think of it like a stack of tasks where functions are added and removed as they execute. 🔹 How the Call Stack Works JavaScript follows a Last In, First Out (LIFO) rule. That means: The last function added to the stack is the first one to finish. Example function first() { second(); } function second() { third(); } function third() { console.log("Hello from third function"); } first(); What happens in the Call Stack 1️⃣ first() is pushed to the stack 2️⃣ second() is called → pushed to the stack 3️⃣ third() is called → pushed to the stack 4️⃣ third() finishes → removed from stack 5️⃣ second() finishes → removed 6️⃣ first() finishes → removed 🔹 Visualising the Stack Call Stack at peak: - third() - second() - first() - Global() Then it unwinds back to the Global Execution Context. 💡 Why This Matters Understanding the call stack helps you understand: - Execution order - Stack overflow errors - Debugging JavaScript - Async behaviour It’s one of the core mechanics of the JavaScript engine. Next post: The Event Loop 🚀 #JavaScript #CallStack #Frontend #WebDevelopment #LearnJS #Programming #LearningInPublic
To view or add a comment, sign in
-
-
PEP TASK-7 🚀 Built a Digital Clock using JavaScript I created a real-time clock application using pure JavaScript, focusing on how time-based functions work behind the scenes. 🔹 What this project demonstrates: • Real-time clock updates using setInterval() • Fetching current time with the JavaScript Date object • Formatting time (HH:MM:SS) dynamically • DOM manipulation to update UI instantly JavaScript makes it possible to build live, interactive features like clocks by continuously updating values every second using functions like setInterval() (Stack Overflow) This project helped me understand how real-time applications work and improved my skills in handling dynamic data in the browser. 💻 Check out the project here: 👉 https://lnkd.in/gWm4YYA5 Would love your feedback! 🙌 #JavaScript #WebDevelopment #Frontend #Coding #StudentDeveloper #Projects #LearningJourney
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