
Understanding variable declarations in JavaScript is crucial for writing clean, predictable, and bug-free Lightning Web Components (LWC) and general JavaScript applications. JavaScript provides three ways to declare variables: var, let, and const. Although they look similar, they behave differently in terms of scope, redeclaration, hoisting, and mutability.
What Are JavaScript Variables?
Variables are used to store data values. In LWC and modern JavaScript, let and const are recommended, while var is considered outdated due to its unpredictable scope behavior.
Watch Our Video Tutorial
Quick Comparison Table
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function Scope | Block Scope | Block Scope |
| Redeclaration | Allowed | Not Allowed | Not Allowed |
| Reassignment | Allowed | Allowed | Not Allowed |
| Hoisting | Hoisted (initialized as undefined) | Hoisted but in Temporal Dead Zone | Hoisted but in Temporal Dead Zone |
| Best Use Case | Avoid using in modern JS | For variables that change | For constants, arrays, objects |
1. var – Function-Scoped (Old JavaScript)
Variables declared using var are function-scoped. That means they are accessible anywhere inside the function. However, var ignores block scope, leading to unexpected behavior.
Example:
if (true) { var x = 10; } console.log(x); // Output: 10 (not block scoped) This can cause bugs in LWC or modern JavaScript apps because variables may leak outside their intended scope.
2. let – Block Scoped (Modern & Recommended)
Variables declared using let are block-scoped, meaning they exist only inside { }.
Example:
if (true) { let y = 20; } console.log(y); // Error: y is not defined This prevents accidental variable leakage, making code cleaner and safer. Let allows reassignment, so it is best for values that change over time.
3. const – Block Scoped Constant
Variables declared using const must be initialized and cannot be reassigned. However, for arrays and objects, the reference is constant, but the internal values can change.
Example:
const user = { name: "Amit" }; user.name = "Rahul"; // Allowed user = {}; // Not Allowed (reassignment error) This makes const ideal for storing stable values or complex data structures.
Hoisting Difference
All three (var, let, const) are hoisted. But only var is initialized as undefined, while let and const remain in the Temporal Dead Zone (TDZ) until execution reaches their declaration.
Example:
console.log(a); // undefined console.log(b); // Error console.log(c); // Error var a = 1; let b = 2; const c = 3; Real-Life Example in Lightning Web Components (LWC)
When building LWCs, let and const help you write predictable, error-free code.
import { LightningElement } from 'lwc'; export default class Sample extends LightningElement { message = 'Hello'; handleClick() { let count = 5; const pi = 3.14; count = 10; // allowed // pi = 4; // not allowed console.log(count); } } Using var in LWC can lead to scope-related bugs and is strongly discouraged.
Best Practices
- Avoid var in modern JavaScript and LWC.
- Use let for variables that change over time.
- Use const for constants, arrays, and objects.
- Name constants in uppercase (optional but recommended).
- Always initialize variables close to where they are used.
Frequently Asked Questions (FAQ)
Conclusion
Understanding the differences between var, let, and const is essential for writing modern JavaScript and building high-quality Lightning Web Components. Follow best practices by avoiding var, using let for dynamic values, and const for stable values. This ensures clean, secure, and reliable code—especially in Salesforce development.
Keep learning with Peoplewoo Skills to master JavaScript, LWC, and Salesforce development!
More SFDC Resources
Start your SFMC journey today — join our Live Training
Need help? Chat with us on WhatsApp anytime.
Learn. Practice. Get Certified. Succeed with Peoplewoo Skills.
