🚀 var vs let vs const in JavaScript If you’re learning JavaScript (or revising fundamentals), understanding these three is a must 👇 🔹 var 1. Function-scoped 2. Can be redeclared & reassigned 3. Hoisted (initialized as undefined) 4. Can cause unexpected bugs 🔹 let 1. Block-scoped {} 2. Can be reassigned, but not redeclared in the same scope 3. Safer than var 🔹 const 1. Block-scoped {} 2 . Cannot be reassigned 3. Must be initialized at declaration 4. Best choice by default ⚠️ Important note: const user = { name: "Alex" }; user.name = "Sam"; // This is allowed const prevents reassignment, not mutation (especially for objects & arrays stored in heap memory). 💡 Best practices: Use const by default Use let when reassignment is needed Avoid var in modern JavaScript 📌 Mastering basics = writing cleaner, bug-free code. #JavaScript #WebDevelopment #ProgrammingBasics #Frontend #LearningToCode
JavaScript var vs let vs const: Scope, Reassignment, and Best Practices
More Relevant Posts
-
Just published a new blog on Array Methods in JavaScript. In this article, I explained: • push() and pop() • shift() and unshift() • map() • filter() • reduce() (simple explanation) • forEach() If you're learning JavaScript, mastering these methods will immediately improve your code quality and readability 👇 https://lnkd.in/gsd7cyU4 Hitesh Choudhary Chai Aur Code Piyush Garg Akash Kadlag Jay Kadlag #JavaScript #WebDevelopment #FrontendDevelopment #LearnInPublic #CodingJourney #100DaysOfCode #WebDev #Programming
To view or add a comment, sign in
-
JavaScript is not “just a scripting language” anymore. It literally runs the web. From a simple button click to complex real-time apps, from animations to full-stack servers — JavaScript is everywhere. But here’s the truth most beginners don’t realize: Learning syntax is easy. Mastering JavaScript thinking is hard. Because JavaScript teaches you how to think in: Asynchronous flows Event-driven logic State management Performance optimization Reusable architecture Anyone can write: "if, else, function" But real skill starts when you can: • debug weird async bugs • manage complex state cleanly • avoid unnecessary re-renders • structure scalable components • write code your future self understands Frameworks change every year. But JavaScript fundamentals stay forever. That’s why I focus more on: Closures Promises & Async/Await ES6+ concepts DOM manipulation Clean logic Because tools come and go. JavaScript thinking stays. Master JavaScript → Everything else becomes easier. #JavaScript #WebDevelopment #FrontendDevelopment #ReactJS #Programming #CleanCode #100DaysOfCode #Developers #SoftwareEngineering #BuildInPublic
To view or add a comment, sign in
-
JavaScript looks simple… until someone asks: "When should you use var, let or const?" 🤔 Understanding this properly separates beginners from confident developers. 🔹 var – Function scoped – Can be reassigned & redeclared – Hoisted (initialized as undefined) – Old school way 🔹 let – Block scoped – Can be reassigned – Cannot be redeclared in same block – Hoisted but in Temporal Dead Zone – Modern best practice for changing values 🔹 const – Block scoped – Cannot be reassigned – Cannot be redeclared – Also in Temporal Dead Zone – Best practice by default 💡 Golden Rule: 👉 Prefer const by default 👉 Use let only when reassignment is needed 👉 Avoid var in modern JavaScript Strong fundamentals in JavaScript save you from hidden bugs and scope-related nightmares later. Which one do you use most in your projects? 👇 #JavaScript #WebDevelopment #FrontendDevelopment #NodeJS #Programming #CodingTips #FullStackDeveloper #LearnToCode #SoftwareEngineering #DeveloperLife
To view or add a comment, sign in
-
-
JavaScript has one of the most famous quirks in programming: 👉 typeof null === "object" Yes… really. But why? Back in the early implementation of JavaScript (1995), values were stored in memory using type tags. Objects had a type tag of 0. Unfortunately, null was represented as a null pointer (0x00). When typeof checked the type tag, it saw 0 and returned "object". That bug shipped. And because fixing it would break massive amounts of existing code, it stayed. So what’s the real issue? Because of this: typeof null === "object" You cannot safely check for objects like this: typeof value !== "object" Why? Because null is not an object - but JavaScript says it is. The correct way to check: if (value !== null && typeof value === "object") { // safe object check } Or more explicitly: if (value && typeof value === "object") (when you’re okay excluding null and other falsy values) This is one of those historical decisions that shaped JavaScript forever. It’s not a feature. It’s a legacy artifact. And knowing it separates beginners from engineers who understand the language deeply. Akash Kadlag Hitesh Choudhary Jay Kadlag Chai Aur Code #JavaScript #WebDevelopment #Programming #LearningInPublic
To view or add a comment, sign in
-
-
I have been continuing my journey through JavaScript internals. After learning prototypes, I moved into more advanced topics. Here is what I have learned recently The .call() method: I found a problem where an inner function "forgets" its context. It’s like making changes in a file but closing it without saving. The solution is using .call(this) to manually pass the context so the data stays where it belongs. Classes & Inheritance: I learned that class is just "syntactic sugar" to hide the main mechanism and make it easy for developers. Behind the scenes, it’s still prototypes and new doing the work. Using extends and super() helps solve the same context problems we solved with .call(). Getters and Setters: These are like layers on a property. Instead of letting code access memory directly, we can use get/set to manipulate values—like sending a salted password instead of the original one. Advanced Object Properties: I found out why we can't overwrite Math.PI. It has a hidden property called writable set to false. We can use Object.defineProperty to give our own objects this same protection. Closures: When we return a function, its whole "lexical scope" (the parent) returns with it. I like to think of it as the function carrying a "backpack" of memory so it remembers variables even after the parent function is done. #LearningInPublic #Javascript #WebDev
To view or add a comment, sign in
-
💡 JavaScript Basics: var, let, and const When learning JavaScript, one of the most common confusions is the difference between var, let, and const. The key differences come down to scope and reassignment. 🔹 var var is function-scoped and does not follow block {} scope. It can be redeclared, which often leads to unexpected bugs. 👉 In modern JavaScript, it’s generally best to avoid using var. 🔹 let let is block-scoped and allows reassignment. It’s ideal for loops and conditional logic where values may change. 🔹 const const is also block-scoped, but its value cannot be reassigned once declared. 👉 Best used for fixed values and safer, more predictable code. ✅ Best practice I follow: • Use const by default • Use let when reassignment is needed • Avoid var whenever possible Small fundamentals like these make a big difference in writing clean, reliable JavaScript. Still learning, still improving 🚀 #JavaScript #FrontendDevelopment #Programming #LearningJourney #WebDevelopment
To view or add a comment, sign in
-
-
🚀 Day 30 of Learning in Public: JavaScript Edition 💻✨ 🌟 Just leveled up my JavaScript skills — dived deep into ES6 Classes & Object-Oriented Programming! 🚀 Classes in JS make code feel more structured, readable and reusable — almost like proper OOP languages, but with JavaScript's unique flavor. Here are the key things I learned & experimented with today: ✅ How to define a class ✅ Creating instances (objects) ✅ Constructor for initialization ✅ Instance variables vs static variables ✅ Getter / setter style methods ✅ Private fields using # (true privacy is still kind of an illusion in JS, but it's getting better!) ✅ Static methods & properties → No constructor overloading (only one constructor allowed) → No protected access modifier — it's either public or private (#) Quick example I played with: ```js class Employee { // Instance fields (public by default) firstName; lastName; // Static field static middleName = "Kumar"; constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } // Instance methods setFirstName(firstName) { this.firstName = firstName; } getFirstName() { return this.firstName; } generateFullName() { const fullName = `${this.firstName} ${this.lastName}`; // fixed typo & used let/const return fullName; } // Static method static printSalary() { return "This is a static method — no instance needed!"; } } const e1 = new Employee("Piyush", "Saxena"); const e2 = new Employee("Amit", "Sharma"); console.log(e1.generateFullName()); // Piyush Saxena console.log(Employee.printSalary()); // This is a static method... console.log(Employee.middleName); // Kumar #JavaScript #WebDevelopment #SoftwareTesting #LearningJourney #ClassInJS #OOPs #ConstructorInJS
To view or add a comment, sign in
-
-
🚀 Post #01 Why modern JavaScript developers prefer "let" over "var" When learning JavaScript, one of the first things many developers notice is that older code uses "var", while modern code uses "let". This shift happened for important technical reasons. 🔹 The problem with "var": Function scope Variables declared with "var" are function-scoped, not block-scoped. This means they can be accessed outside the block where they were created, which can lead to unexpected behavior and bugs. Example: if (true) { var x = 10; } console.log(x); // 10 ❌ (still accessible) 🔹 The advantage of "let": Block scope Variables declared with "let" are block-scoped, meaning they only exist inside the block where they are defined. This makes code more predictable and safer. Example: if (true) { let y = 20; } console.log(y); // Error ✅ (correct behavior) 🔹 Why modern developers use "let": • Prevents accidental variable access • Reduces bugs caused by scope confusion • Makes code cleaner and easier to maintain • Follows modern JavaScript (ES6+) standards 📌 Conclusion: "var" is not completely removed, but it is considered outdated in modern development. Today, developers use "let" (and "const") to write safer, more reliable, and professional JavaScript code. #JavaScript #WebDevelopment #Programming #Coding #LearningJourney
To view or add a comment, sign in
-
-
Stop Googling JavaScript basic syntax. Start coding with confidence. 📒⚡ JavaScript is vast, and trying to memorize every method and operator is a waste of brainpower. The best developers don't memorize—they reference. What’s inside? ✅ The Basics: Variables, Data Types, and Operators. ✅ Control Flow: if/else, switch, and Loops (for, while). ✅ Functions: Arrow functions vs. Function declarations. ✅ Data Structures: Working with Arrays and Objects efficiently. ✅ DOM Manipulation: How to select and modify HTML elements.. ✅ Modern JS: Essential ES6+ features like Destructuring and Template Literals. Swipe left to save this reference for your next project! ⬅️ 💡 Found this helpful? Follow Rensith Udara Gonalagoda for premium web development insights. 🚀 Repost to help your network stay updated. 🔁 Comment "Saved" if this is going in your bookmarks! 👇 #javascript #webdevelopment #cheatsheet #coding #frontend #codewithalamin #webdeveloper #programming #jsbasics #codingresources
To view or add a comment, sign in
-
Most developers use JavaScript loops every day — but very few truly understand the difference between for, for…of, and for…in. This week I revisited these fundamentals along with one of JavaScript’s most misunderstood concepts: hoisting — and it completely changes how you reason about code execution. Here’s the simple mental model: 🔹 for loop → when you need full control (index, performance, custom logic) 🔹 for…of → clean value iteration for arrays and iterable data 🔹 for…in → object key traversal (not ideal for arrays) 🔹 Hoisting → JavaScript prepares declarations before execution, which explains many “weird” behaviors with var, let, const, and functions Understanding these concepts is not about syntax — it’s about how the JavaScript engine thinks. When you grasp that JavaScript runs in a compilation phase before execution, topics like scope, closures, TDZ, and async behavior start making real sense. Sometimes going back to fundamentals gives the biggest upgrade in thinking. What JavaScript concept felt simple at first but later turned out to be deep for you? #JavaScript #WebDevelopment #Frontend #Programming #SoftwareEngineering #Coding #NodeJS #React #LearningInPublic https://lnkd.in/g9TRur-S
JavaScript Loops & Hoisting Explained — for, for…of, for…in
https://www.youtube.com/
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