Difference Between var, let & const in JavaScript | Peoplewoo Skills

23.11.25 05:57 PM - By Peoplewoo

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: varlet, 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


Featurevarletconst
ScopeFunction ScopeBlock ScopeBlock Scope
RedeclarationAllowedNot AllowedNot Allowed
ReassignmentAllowedAllowedNot Allowed
HoistingHoisted (initialized as undefined)Hoisted but in Temporal Dead ZoneHoisted but in Temporal Dead Zone
Best Use CaseAvoid using in modern JSFor variables that changeFor 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)

1. Should we use var in LWC?

No. var can cause scope leakage and unpredictable behavior. Use let or const instead.

2. Can we reassign a const variable?

No. You can modify properties of arrays/objects but cannot reassign the variable itself.

3. What is the Temporal Dead Zone?

The period between variable hoisting and initialization where let and const cannot be accessed.

4. Does let support block scoping?

Yes. let variables exist only inside the block { } where they are defined.

5. Which declaration is best for performance?

const is recommended whenever possible because it prevents reassignment and makes code more predictable.

Conclusion

Understanding the differences between varlet, 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.

Peoplewoo

Peoplewoo

Peoplewoo Consulting Private Limited
https://www.peoplewoo.com/