🤯 𝐓𝐡𝐞 𝐕𝐚𝐫𝐢𝐚𝐛𝐥𝐞 𝐕𝐢𝐛𝐞 𝐂𝐡𝐞𝐜𝐤: 𝒗𝒂𝒓 𝐯𝐬. 𝒍𝒆𝒕 𝐯𝐬. 𝒄𝒐𝒏𝒔𝒕 If you've ever felt like your JavaScript variables were causing trouble, you might be right! Choosing the wrong declaration keyword can lead to bugs, leaks, and general chaos. Here is the quick, no-nonsense guide to who these three are and when to use them: 1. 𝒗𝒂𝒓(𝐓𝐡𝐞 𝐓𝐫𝐨𝐮𝐛𝐥𝐞𝐦𝐚𝐤𝐞𝐫) 🚫 𝐒𝐜𝐨𝐩𝐞: 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧-𝐬𝐜𝐨𝐩𝐞𝐝 (it leaks out of loops and blocks). 𝐑𝐞-𝐚𝐬𝐬𝐢𝐠𝐧𝐦𝐞𝐧𝐭: Allowed. 𝐓𝐡𝐞 𝐋𝐞𝐬𝐬𝐨𝐧: A legacy keyword. 𝐃𝐨𝐧'𝐭 𝐮𝐬𝐞 𝐢𝐭 in modern code. 2. 𝒍𝒆𝒕 (𝐓𝐡𝐞 𝐅𝐥𝐞𝐱𝐢𝐛𝐥𝐞 𝐎𝐧𝐞) 📝 𝐒𝐜𝐨𝐩𝐞: 𝐁𝐥𝐨𝐜𝐤-𝐬𝐜𝐨𝐩𝐞𝐝 ({ } are its boundaries). 𝐑𝐞-𝐚𝐬𝐬𝐢𝐠𝐧𝐦𝐞𝐧𝐭: Allowed (you can change the value). 𝐓𝐡𝐞 𝐋𝐞𝐬𝐬𝐨𝐧: Use it for variables where the value 𝐧𝐞𝐞𝐝𝐬 𝐭𝐨 𝐜𝐡𝐚𝐧𝐠𝐞 (like a counter in a loop or a score). 3. 𝒄𝒐𝒏𝒔𝒕 (𝐓𝐡𝐞 𝐃𝐞𝐟𝐚𝐮𝐥𝐭 𝐂𝐡𝐨𝐢𝐜𝐞) ✅ 𝐒𝐜𝐨𝐩𝐞: 𝐁𝐥𝐨𝐜𝐤-𝐬𝐜𝐨𝐩𝐞𝐝. 𝐑𝐞-𝐚𝐬𝐬𝐢𝐠𝐧𝐦𝐞𝐧𝐭: 𝐍𝐨𝐭 𝐚𝐥𝐥𝐨𝐰𝐞𝐝 (the binding is permanent, though object properties can still change). 𝐓𝐡𝐞 𝐋𝐞𝐬𝐬𝐨𝐧: This should be your 𝐝𝐞𝐟𝐚𝐮𝐥𝐭 𝐜𝐡𝐨𝐢𝐜𝐞 for almost everything. Use it unless you know the value will be updated. 𝐓𝐡𝐞 𝐆𝐨𝐥𝐝𝐞𝐧 𝐑𝐮𝐥𝐞: Start with const. If you get an error because you need to update the variable later, and only then, switch to let. Save yourself the debugging headache! What percentage of the time do you find yourself using const? I'm aiming for 90% plus! #JavaScript #WebDevTips #CodingBestPractices #FrontEndDevelopment #constletvar
"JavaScript Variables: var vs let vs const"
More Relevant Posts
-
🔥 JS Loops Demystified: for vs for…in vs for…of If you’ve ever wondered which loop to use in JavaScript, you’re not alone. Choosing the wrong one can cause subtle bugs! Here’s a quick guide: 1️⃣ Classic for loop – The old reliable const arr = [10, 20, 30]; for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } Use when you need index access, or want full control over iteration. 2️⃣ for…in loop – Iterate over keys (property names) const arr = [10, 20, 30]; for (let key in arr) { console.log(key); // 0, 1, 2 } Works on objects too. ⚠️ Can be dangerous for arrays — also loops over inherited properties. 3️⃣ for…of loop – Iterate over values const arr = [10, 20, 30]; for (let value of arr) { console.log(value); // 10, 20, 30 } Perfect for arrays, strings, maps, sets. Safe, clean, and readable. 💡 Pro tip: Use for when you need the index. Use for…of for values (most array iterations). Avoid for…in on arrays — reserve it for objects. Small choice, huge impact on readability and bug prevention. #JavaScript #CodingTips #WebDevelopment #FrontendDev #CleanCode #JSProTips #ForOfVsForIn
To view or add a comment, sign in
-
💛 𝗗𝗮𝘆 𝟮 — 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁: 𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀, 𝗦𝗰𝗼𝗽𝗲, 𝗮𝗻𝗱 𝗛𝗼𝗶𝘀𝘁𝗶𝗻𝗴 Today, I revisited one of the most confusing yet powerful concepts in JavaScript — 𝗵𝗼𝗶𝘀𝘁𝗶𝗻𝗴 and 𝘀𝗰𝗼𝗽𝗶𝗻𝗴. 🧠 💡 𝗖𝗼𝗻𝗰𝗲𝗽𝘁: JavaScript moves all declarations to the top of their scope during compilation — this is known as hoisting. However, the behavior differs based on whether you use var, let, const, or functions. 💻 𝗖𝗼𝗱𝗲 𝗦𝗻𝗶𝗽𝗽𝗲𝘁: console.log(a); // undefined (hoisted) var a = 10; console.log(b); // ❌ ReferenceError (in TDZ) let b = 20; sayHello(); // ✅ Works — function declarations are hoisted function sayHello() { console.log("Hello from a hoisted function!"); } // ❌ Error: sayHi is not a function sayHi(); var sayHi = function () { console.log("Hi from function expression!"); }; 🧩 𝗘𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻: 0. var is hoisted and initialized with undefined. 1. let and const are hoisted but stay uninitialized in the temporal dead zone (TDZ). 2. Function declarations are fully hoisted, so you can call them before defining. 3. Function expressions (especially when assigned to var) behave like variables — hoisted but not initialized. 📈 𝗦𝗰𝗼𝗽𝗲 𝗪𝗶𝘀𝗱𝗼𝗺: var → function scope let & const → block scope Functions → create their own local scope 🔥 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆: Understanding how JavaScript creates and executes contexts will help you debug faster and think more like the JS engine itself. #JavaScript #100DaysOfCode #Hoisting #Scope #Functions #FrontendDevelopment #LearningEveryday
To view or add a comment, sign in
-
🧩 Undefined vs Null 🤔 ✨ When there’s no existence, it’s Undefined, whereas emptiness exists by choice, it’s Null. 🔹 undefined → When a variable is declared but not assigned by the user, JavaScript itself assigns the value undefined by default. let a; console.log(a); // undefined 🧠 It means: “No value exists yet — JavaScript couldn’t find one.” 🔹 null → When a variable is explicitly assigned by the user to represent emptiness, it holds the value null. let b = null; console.log(b); // null 💡 It means: “The developer intentionally set this to nothing.” ⚙️ Type check curiosity typeof null; // "object" ❗ (a known JavaScript bug) typeof undefined; // "undefined" 🚫 Falsy values Both are falsy during condition checks 👇 if (!undefined && !null) console.log('Falsy values!'); 🎯 In short: 🟠 undefined → Not assigned by the user → JavaScript assigns it automatically. 🟣 null → Explicitly assigned by the user → JavaScript doesn’t assign it. 🔖 Hashtags #JavaScript #WebDevelopment #Frontend #CodingTips #LearnToCode #JSBasics #WebDevCommunity #JavaScriptTips #CodeNewbie #DeveloperInsights
To view or add a comment, sign in
-
🚀 Ever wondered why a for loop with var logs 3,3,3 — but the same loop with let logs 0,1,2? It’s not magic — it’s JavaScript’s scoping and binding behavior at play 👇 🧩 The Classic Mystery for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } // Output: 3, 3, 3 for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } // Output: 0, 1, 2 💡 The Hidden Truth var is function-scoped, so it creates one single binding for the entire loop. ➜ All 3 callbacks share the same i. ➜ By the time they run, i = 3. let is block-scoped — and in a for loop, JavaScript creates a fresh binding per iteration. ➜ Each callback remembers its own copy of i. ➜ So it prints 0, 1, 2. ⚙️ Behind the Scenes You can imagine it like this: 🧠 var → One shared box 🧳 🧠 let → A new box every iteration 📦 When the async setTimeout runs later, all var callbacks open the same box (value = 3), while each let callback opens its own box (0, 1, 2). ✅ What You Should Use In modern JavaScript: 🔹 Always prefer let or const over var. They give cleaner scoping, fewer bugs, and predictable async behavior. 💬 Have you ever debugged this “3,3,3” issue before? Drop your story or how you solved it ⬇️ #JavaScript #WebDevelopment #Coding #JSDeepDive #Frontend #Developers #LearnInPublic
To view or add a comment, sign in
-
-
🌀 𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗣𝗿𝗼𝘅𝗶𝗲𝘀 — 𝗜𝗻𝘁𝗲𝗿𝗰𝗲𝗽𝘁, 𝗖𝗼𝗻𝘁𝗿𝗼𝗹 & 𝗣𝗼𝘄𝗲𝗿 𝗨𝗽 𝗬𝗼𝘂𝗿 𝗢𝗯𝗷𝗲𝗰𝘁𝘀 Wrappers, traps, handlers — the native Proxy object in JavaScript gives you 𝗺𝗲𝘁𝗮-𝗰𝗼𝗻𝘁𝗿𝗼𝗹 over how your objects behave. Want to intercept property access, validate assignments, or log operations? Proxies unlock that power. In this post I break down: ✅ What Proxy objects really do (the target + handler + traps model) ✅ How traps like `get`, `set`, `apply`, `construct` let you intercept reads, writes, function calls and constructor calls ✅ Real-world use cases: validation, access control, logging, lazy initialization, smart defaults 👉 Read here: https://lnkd.in/d2z-7z6w Stay curious. Code smarter. 🧠 #JavaScript #WebDevelopment #Proxies #Metaprogramming #CodingTips
To view or add a comment, sign in
-
🌀 𝗥𝗲𝗳𝗹𝗲𝗰𝘁 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 — 𝗧𝗵𝗲 𝗦𝗲𝗰𝗿𝗲𝘁 𝗣𝗮𝗿𝘁𝗻𝗲𝗿 𝗼𝗳 𝗣𝗿𝗼𝘅𝗶𝗲𝘀 In this article I explore how the native `Reflect` object works hand-in-hand with `Proxy` to give you meta-control over object operations. ⚙️ Here’s what you’ll take away: ✅ What `Reflect` really does — giving you transparent, predictable operations like `get`, `set`, `deleteProperty`, etc. ✅ Why `Reflect` is often 𝘂𝘀𝗲𝗱 inside `Proxy` traps to forward default behaviour (so you intercept only what you need). ✅ Real-world patterns: logging access, enforcing rules, building reactive/decorated objects. 👉 𝗥𝗲𝗮𝗱 here: https://lnkd.in/dNge-Se6 Stay curious. Code smarter. 🧠 #JavaScript #WebDevelopment #Reflect #Proxies #Metaprogramming #CodingTips
To view or add a comment, sign in
-
💡 Finding the Middle of a Linked List — The Clean JavaScript Way When working with linked lists, one of the classic questions is: “How do you find the middle node?” The obvious solution is to: 1️⃣ Traverse once to count all nodes. 2️⃣ Traverse again to reach the middle. That works — but it’s not optimal. 🚀 Here’s the elegant two-pointer technique in JavaScript: function findMiddle(head) { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; } return slow; // middle node } ✨ The idea: fast moves twice as fast as slow. When fast hits the end, slow is right in the middle. One pass. Clean logic. No extra space. This trick isn’t just useful for linked lists — it’s a great reminder that thinking in pointers often leads to the most elegant algorithms. #JavaScript #DataStructures #Algorithms #CodingTips #LinkedList #ProblemSolving #TechLearning
To view or add a comment, sign in
-
🚀 5 Quick Facts About Arrays in JavaScript 1️⃣ Dynamic & Flexible – You can store numbers, strings, or even objects in the same array. 🧩 const arr = [1, "two", true]; 2️⃣ Technically Objects – typeof [] returns "object", not "array". ✅ Use Array.isArray(arr) to be sure. 3️⃣ Built for Efficiency – Use map(), filter(), and reduce() to write clean, functional code. 4️⃣ Spread Operator Power – Combine or clone arrays effortlessly. ⚙️ const merged = [...arr1, ...arr2]; 5️⃣ Zero-Indexed – The first element starts at index 0. 📍 arr[0] gives the first item. Arrays are more powerful than they look — master them, and your JS code will be cleaner, faster, and smarter 💡 #JavaScript #WebDevelopment #CodingTips #Arrays #DeveloperLife
To view or add a comment, sign in
-
🚀 Day 88/90 – #90DaysOfJavaScript Topic covered: Today I explored how JavaScript handles String & Number primitives vs String & Number objects.s ✅ String Primitive vs String Object 👉 "Hello" → primitive (lightweight, fast) 👉 new String("Hello") → wrapper object (rarely needed) 👉 JS temporarily wraps primitives to access methods like .length, .slice(), .repeat() ✅ Number Constructor 👉 new Number("34") → Number object (typeof → "object") 👉 Number("34") → converts to number (typeof → "number") ✅ Type Conversion Rules 👉 Number("45") → ✅ 45 👉 Number("abc") → ❌ NaN 👉 Number(true) → ✅ 1 👉 Number(null) → ✅ 0 👉 Number(undefined) → ❌ NaN 👉 Arrays & objects behave differently in numeric conversion ✅ Key Rule 👉 Always use Number(value) for conversion ❌ Avoid new Number() unless absolutely necessary 🧠 Takeaway: JS primitives are faster & efficient — wrapper objects only exist to give methods to primitives. Prefer primitives for real-world use. 🛠️ Access my GitHub repo for all code and explanations: 🔗 https://lnkd.in/e347pqH7 Let’s learn together! Follow my journey to #MasterJavaScript in 90 days! 🔁 Like, 💬 comment, and 🔗 share if you're learning too. #JavaScript #WebDevelopment #CodingChallenge #Frontend #JavaScriptNotes #MasteringJavaScript #GitHub #LearnInPublic
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