JavaScript is a widely used programming language for making websites and web applications. In the Salesforce world, it’s crucial for creating Lightning Web Components (LWC). When you’re using JavaScript in LWC, it’s important to know the difference between ‘let’ and ‘var’ and ‘const’ because they are used to create variables in slightly different ways.
Var :
1. Function Scope : In JavaScript, when you use the ‘var’ keyword to declare a variable, it’s scoped to the function it’s created in. This means it’s only accessible within that function and any functions nested inside it.
function exampleFunction() { var x = 10; if (true) { var y = 20; // Accessible outside the if block console.log(x); // Output: 10 } console.log(y); // Output: 20 }
2. Hoisting : Another thing about ‘var’ is that the variable declarations using it are “hoisted.” This means they’re moved to the top of their scope during the code execution, so you can access them before they’re even declared in the code.
console.log(a); // Output: undefined var a = 5; console.log(a); // Output: 5 3. Re-declaration and Update : Variables declared with var
can be re-declared and updated within the same scope without causing errors.
var age = 25; var age = 30; // This is allowed, age is updated console.log(age); // Output: 30 function exampleFunction() { var x = 10; var x = 15; // This is also allowed console.log(x); // Output: 15 }
Let :
1. Block Scope : Variables declared with let
are confined to the block in which they are declared. A block could be a loop, a condition, a function, or any code block enclosed within curly braces.
if (true) { let x = 10; // Accessible only within this if block console.log(x); // Output: 10 } console.log(x); // Error: x is not defined
2. No Hoisting : Unlike variables declared with var
, let
variables are not hoisted to the top of their scope. They cannot be accessed before they’re declared in the code.
console.log(a); // Error: Cannot access ‘a’ before initialisation let a = 5; console.log(a); // Output: 5
3. No Re-declaration, but Update Allowed : Variables declared with let
cannot be re-declared within the same scope. However, they can be updated with new values.
let age = 25; let age = 30; // Error: Identifier ‘age’ has already been declared let value = 10; value = 15; // This is allowed, updating the value console.log(value); // Output: 15
By using let
, you ensure more predictable and less error-prone code behavior, especially in situations where you want clear control over variable scoping and avoid unintentional re-declarations.
Const
1. Constant Value : Variables declared with const
are used to hold values that remain constant throughout their scope. Once assigned, the value cannot be changed or reassigned.
const pi = 3.14159; pi = 3.14; // Error: Assignment to constant variable
2. Block Scope : Similar to let
, const
variables also have block-level scope, meaning they are accessible only within the block in which they are defined.
if (true) { const x = 10; // Accessible only within this if block console.log(x); // Output: 10 } console.log(x); // Error: x is not defined
3. No Hoisting : Just like with let
, const
variables are not hoisted to the top of their scope and cannot be accessed before they’re declared.
console.log(a); // Error: Cannot access ‘a’ before initialisation const a = 5; console.log(a); // Output: 5
4. Cannot Be Reassigned : The primary difference between const
and let
is that once a value is assigned to a const
variable, it cannot be changed or reassigned. This is particularly useful for variables that should remain constant.
const name = “John”; name = “Jane”; // Error: Assignment to constant variable
5. Immutable Objects and Mutability : When using const
with objects, the variable itself remains constant, but the properties of the object can still be modified. This is because const
ensures the reference to the object doesn’t change, but it doesn’t prevent changes to the object’s content.
const person = { name: “Alice” }; person.name = “Bob”; // This is allowed, person still points to the same object person.age = 30; // This is also allowed, adding a new property
Using const
is recommended for values that are intended to remain constant, providing clear intent and helping prevent accidental value changes.
What is the problem with using var keyword ?
The main problem with using the var
keyword in JavaScript was its scoping behaviour, which could lead to unintended and sometimes hard-to-debug issues. Let’s explore the problem with an example:
function varExample() { for (var i = 0; i < 5; i++) { // Some code here } console.log(i); // Output: 5 }
In this example, you might expect that the variable i
would only be accessible within the for
loop since it was declared inside it. However, due to the function-scoped nature of var
, the variable i
remains accessible even outside the loop, which might not be the desired behavior.
This behaviour can lead to unintended side effects when working with loops, conditions, or nested functions, where variables might end up affecting the broader scope in ways you didn’t anticipate.
In contrast, using let
or const
would provide block-level scoping, preventing such unintended scope leakage:
function letExample() { for (let i = 0; i < 5; i++) { // Some code here } console.log(i); // Error: i is not defined } With let
, the variable i
is only accessible within the for
loop, as expected. This helps in writing more predictable and maintainable code.
Conclusion :
In Lightning Web Components (LWC), it’s better to use “let” when you create variables instead of using “var”. This is because “let” helps you control where you can use the variable and stops accidental changes to variables that other parts of your code might be using.
With “let”, the variable you create only works in the specific part of your code where you made it. This makes it simpler to understand and fix any problems in your code because the variable’s effects are only where you put it.
By picking “let” instead of “var” in LWC, you make your code more organised and easier to work with. This choice fits well with how Lightning Web Components are designed.