🚀 Understanding var, let, and const in JavaScript In JavaScript, we use var, let, and const to declare variables — but they behave differently. Knowing when to use which one can make your code cleaner and bug-free. 👇 🔹 var Introduced in ES5 (older way). Function-scoped — accessible within the entire function where it’s declared. Can be redeclared and reassigned. Hoisted to the top of its scope (but initialized as undefined). var name = "Abdul"; var name = "Hak"; // ✅ Redeclaration allowed console.log(name); // Output: Hak 🔹 let Introduced in ES6 (2015). Block-scoped — only accessible inside { } where it’s defined. Can be reassigned but not redeclared within the same scope. Hoisted but not initialized (accessing before declaration gives an error). let age = 25; age = 26; // ✅ Allowed // let age = 27; ❌ Not allowed console.log(age); 🔹 const Also introduced in ES6. Block-scoped like let. Cannot be reassigned or redeclared. Must be initialized at the time of declaration. const country = "India"; // country = "USA"; ❌ Not allowed console.log(country); 💡 Pro Tip: Use let and const instead of var in modern JavaScript — they make your code safer and more predictable. #JavaScript #WebDevelopment #Coding #LearnToCode #FrontendDevelopment
There is really no need for var to be used in 2025.
Here is the detail blog about JavaScript variables. https://wenowadays.com/blogs/what-are-variables-in-javascript-let-const-and-var-explained-simply