var vs let vs const — The Real Differences If you're still reaching for `var` by default, this one's for you. Three keywords. Three very different behaviors. var • Function-scoped (ignores block scope) • Can be redeclared in the same scope • Hoisted and initialized as undefined • Legacy. Avoid in new code. let • Block-scoped • Can be reassigned, cannot be redeclared • Hoisted but not initialized (Temporal Dead Zone) • Use it when a value needs to change. const • Block-scoped • Cannot be reassigned or redeclared • Same hoisting behavior as let • Use it by default. Your future self will thank you. Rule of thumb: → const by default → let when you must reassign → var — don't Next post: Data Types — all seven, with the typeof null trap. Follow the series. #JavaScript #WebDevelopment #Frontend #CleanCode #NodeJS #SoftwareEngineering #WebDev #CodingTips #ES6 #LearnJavaScript #Programming #Coder #FullStackDeveloper #ReactJS #TypeScript #100DaysOfCode #DeveloperCommunity #TechTips #CodeNewbie #ProgrammingLife
var vs let vs const in JavaScript
More Relevant Posts
-
LeetCode Day 19 : Problem 167 (Two Sum II - Input Array Is Sorted) Just solved my LeetCode problem for today. It was "Two Sum II", sorted array, constant space, find two numbers that add up to a target. Sounds like a simple two-pointer problem, right? But here's what I actually learned: My first instinct was to write three separate while loops, each moving a different pointer in a different direction, thinking I was covering all the cases. I wasn't. The bugs were layered. The first loop only moved the right pointer. The second only moved the left. The third moved both but only toward the center. No single loop ever explored the full space of valid pairs. There was also a check I wrote: if (arr !== undefined) return arr. Looked like a guard. Did absolutely nothing. An empty array is never undefined in JavaScript. The condition was always true and I never noticed. The real fix wasn't patching the loops. It was understanding why the sorted order makes three loops completely unnecessary. The two-pointer solution: start left at index 0, right at the last index. If the sum is too small, move left forward. If the sum is too big, move right backward. One loop, every pair covered, O(n) time, O(1) space. The sorted order is the key insight. It guarantees you never need to backtrack, which is exactly why one clean loop beats three broken ones. The real lesson? When your instinct is to add more loops to cover more cases, stop. Ask whether the structure of the data already tells you which direction to move. Sometimes the constraint in the problem is the solution. #DSA #LeetCode #JavaScript #CodingJourney #Programming
To view or add a comment, sign in
-
-
LeetCode Day 10 : Problem 380 (Insert Delete GetRandom O(1)) Just solved my tenth LeetCode problem. It was "Insert Delete GetRandom O(1)", sounds like a basic Set problem, right? But here's what I actually learned: My first attempt used only a Map. Insert and remove worked fine, Map gives you O(1) for both. So I thought I was done. Then came getRandom(). I wrote Array.from(this.map.keys()) to pick a random element. It worked. Tests passed locally. But it was O(n), rebuilding an entire array from the map on every single call. The problem explicitly requires O(1) for all three operations. My solution was silently failing the constraint. I also had a crash hiding in insert. I wrote this.map.insert(val), but JavaScript's Map has no insert() method. The correct method is .set(). One wrong method name and the whole class throws a TypeError at runtime. The real fix wasn't patching getRandom(). It was rethinking the data structure entirely. The trick: maintain both a Map and an Array together. The Array holds the actual values so getRandom() is just a random index lookup, pure O(1). The Map stores each value's index in the array so insert and remove stay O(1) too. The hardest part? Remove. You can't just delete from the middle of an array in O(1). The solution: swap the target element with the last element, pop the end, then update the swapped element's index in the Map. No shifting, no gaps. Two bugs in one problem. One crashed the code, one passed tests but broke the core constraint. The real lesson? Passing test cases is not the same as meeting complexity requirements. Always verify your Big O, not just your output. #DSA #LeetCode #JavaScript #CodingJourney #Programming
To view or add a comment, sign in
-
-
📘 Big O Notation I’ve been spending some time understanding Big O Notation — it felt confusing at first, but it’s becoming clearer little by little. Big O is a way to understand how efficient our code is as the input size grows. It’s not about exact speed, but about how well an algorithm scales. 💡What I practiced: • Learned the basics like O(1), O(log n), O(n), and O(n²) • Compared how arrays and sets behave in different operations • Used simple loops, map, and forEach to see how performance changes • Wrote small examples to better understand the differences 🧠 My takeaway: Just because code works doesn’t mean it’s efficient. Thinking about performance from the beginning really matters. Still learning, but getting more comfortable with these concepts. #BigONotation #TimeComplexity #CodingJourney #JavaScript #LearningEveryday
To view or add a comment, sign in
-
-
The 2nd edition of "Generators in JavaScript" is out. This one's been rewritten with clearer explanations, better exercises, and patterns that reflect how generators are actually used in production today. What's inside: - How generators work under the hood - Practical patterns: lazy sequences, data pipelines, bidirectional control flow - The async runner pattern that async/await was built on - Error handling strategies for suspended generators - Composition with yield* and generator pipelines - A quick reference appendix you'll actually keep open while coding Every example is runnable. Every chapter has exercises with worked solutions. If you're a JS/TS developer comfortable with promises and async/await but you've never really dug into generators, this book fills that gap. And if you read the first edition, the 2nd is a meaningful upgrade. Link in the comments. #JavaScript #WebDev #Generators #AsyncJS #Programming
To view or add a comment, sign in
-
-
Maps and Sets in JavaScript: A Deep Dive JavaScript provides several ways to store and manage data, with objects and arrays being the most commonly used structures. However, when dealing with unique values or key-value pairs with better effi... Read more → https://lnkd.in/dvP2-4nH #TheCampusCoders #Tech #Developers #Programming #WebDev
To view or add a comment, sign in
-
📘 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐌𝐨𝐝𝐮𝐥𝐞 (𝐁𝐚𝐬𝐢𝐜) 𝐒𝐞𝐜𝐭𝐢𝐨𝐧 6: 𝐀𝐫𝐫𝐚𝐲 & 𝐋𝐨𝐨𝐩𝐬 🔹 𝐀𝐫𝐫𝐚𝐲 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 1.What is array / collection / list? Why use it? 2.Array syntax / How to declare an array? 3.What is array index? 4.Array index starts from? 5.How to access element? 6.How to set value? 7.Findout Array length / How to find total length? 8.Array methods? 9.What is array method? 10.Why use array method? 🔹 𝐋𝐨𝐨𝐩 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 1.What is loop? 2.Why use loop? 3.Loop structure? 4.Types of loop? 5.Array loop / Array specific loops? 6.Array is immutable or mutabl? 7.What is loop iteration? 8.Loop control statements? 9.What is array of object? 10.Why use array of object? 🎯 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 (𝐄𝐱𝐭𝐫𝐚) 1.What is the main difference between slice() and splice()? 1.What is the difference between map() and forEach()? (Which one should you use when?) 2.What is the difference between filter() and find()? (Which one should you use when?) 3.When to use reduce()? 4.Difference between for...in() and for...of()? #DotNet #AspNetCore #MVC #FullStack #SoftwareEngineering #ProgrammingTips #DeveloperLife #LearnToCode #JavaScript #JS #JavaScriptTips #JSLearning #FrontendDevelopment #WebDevelopment #CodingTips #CodeManagement #DevTools
To view or add a comment, sign in
-
-
TypeScript + WebAssembly = Mind Blown 🤯 Just learned about AssemblyScript and I'm kicking myself for not knowing this earlier. You know that feeling when your image processing or data viz just... lags? JavaScript is great, but for heavy computations, it hits a wall. Turns out there's a TypeScript-like language called AssemblyScript that compiles straight to WebAssembly. Companies are reporting 4-7x faster performance for math-heavy tasks. The catch? It's not exactly TypeScript - you need to rewrite with type annotations and different primitives. But the syntax is familiar enough that the learning curve isn't brutal. For anyone building real-time features, data processing pipelines, or browser-based tools that need serious performance - this might be worth exploring. #WebDev #TypeScript #WebAssembly #Performance
To view or add a comment, sign in
-
-
🚀 Day 968 of #1000DaysOfCode ✨ Types of Loops in JavaScript (Explained Simply) Loops are one of the most fundamental concepts in JavaScript — but choosing the right one can make a big difference in your code. In today’s post, I’ve explained the different types of loops in JavaScript in a simple and practical way, so you can understand when to use each one. From `for` and `while` to `for...of` and `for...in`, each loop has its own purpose depending on how you’re working with data. Using the right loop not only makes your code cleaner but also improves readability and performance in many cases. This is one of those basics that every developer uses daily — but mastering it helps you write much better code. If you’re working with arrays, objects, or complex data structures, this is something you should be confident about. 👇 Which loop do you use the most in your day-to-day coding? #Day968 #learningoftheday #1000daysofcodingchallenge #FrontendDevelopment #WebDevelopment #JavaScript #React #CodingCommunity #JSBasics
To view or add a comment, sign in
-
🔥 JavaScript Arrays — Hidden Performance Cost You Might Ignore Hey devs 👋 We all use array methods daily: .map() .filter() .reduce() But here’s something most developers don’t think about 👇 👉 Chaining multiple methods: arr.filter(...).map(...).reduce(...) Looks clean… but: ❌ Creates multiple intermediate arrays ❌ Increases memory usage ❌ Impacts performance on large data 💡 Better approach (when needed): ✔ Combine logic in a single loop ✔ Use reduce smartly ✔ Optimize only for large datasets ⚡ Senior rule: “Readable code first… optimized code when necessary.” 👉 Insight: Not every clean-looking code is efficient. Have you optimized array-heavy logic before? #javascript #performance #webdevelopment #programming #frontend #backend #softwareengineering #Coding #TechCareers #Programming #success
To view or add a comment, sign in
-
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗦𝗵𝗮𝗹𝗹𝗼𝘄 𝗖𝗼𝗽𝘆 𝘃𝘀 𝗗𝗲𝗲𝗽 𝗖𝗼𝗽𝘆 📦 If you’ve ever updated state and something weird happened… this might be why 👇 🔹 Shallow Copy → copies only the first level 🔹 Nested objects are still referenced (same memory) Example: ➡️ Using { ...obj } or Object.assign() 💡 Problem: Change a nested value… and you might accidentally mutate the original object 😬 🔹 Deep Copy → copies everything (all levels) 🔹 No shared references 🔹 Safe to modify without side effects Example: ➡️ structuredClone(obj) ➡️ or libraries like lodash ⚠️ The common pitfall: You think you made a copy: ➡️ { ...user } But inside: ➡️ user.address.city is STILL linked So when you update it: ❌ You mutate the original state ❌ React may not re-render correctly ❌ Bugs appear out of nowhere 🚀 Why this matters (especially in React): State should be immutable ➡️ Always create safe copies ➡️ Avoid hidden mutations ➡️ Keep updates predictable 💡 Rule of thumb: 🔹 Flat objects? → shallow copy is fine 🔹 Nested data? → consider deep copy Understanding this difference = fewer bugs + cleaner state management And yes… almost every developer gets burned by this at least once 😄 Sources: - JavaScript Mastery - w3schools.com Follow 👨💻 Enea Zani for more #javascript #reactjs #webdevelopment #frontend #programming #coding #developers #learnjavascript #softwareengineering #100DaysOfCode
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