Clarification is needed on the usage of var and let-in suitelet scripts. Usage of which variable is better in suitelet scripts.
Clarification on the below-mentioned sentence
In Suitelet “Var” is used instead of “let” because the logic applied globally doesn’t work well in “let”
Solution:
In JavaScript, we can declare variables in three different ways like this:
// Without keywords. It is essentially the same as var
// and not allowed in 'strict' mode.
name = 'Jack';
// Using var
var price = 100;
// Using let
let isPermanent = false;
// Using const
const PUBLICATION = 'freeCodeCamp'
It is best when you understand var, let, and const with these three concepts:
- Scope
- Reassigning a new value
- When you access a variable before declaring it
These keywords differ in usage with respect to these concepts.
Variable Scope in JavaScript
In JavaScript, we use scope as a way to identify where and whether we can use a variable. The variables may exist within a block, inside a function, or outside a function and block.
So, what is a block? A block (that is, a code block) is a section of the code we define using a pair of curly braces ({…}). Something like this:{ let name = "alex"; }
Here is a function with the name test.function test() { let name = "alex"; }
Anything and everything outside of a block or a function we’ll call Global. So, when we declare variables, they can exist within a block, inside a function, or outside of a block/function – that is, they have global scope.
How to Use JavaScript Variables in Block Scope
If you do not want a variable declared inside a { } block to be accessed outside of the block, you need to declare it using the let or const keywords. Variables declared with the var keyword inside the { } block are accessible outside of the block too. So, be careful. Do not use the var keyword inside a block (block scope). Always use let and const instead.
How to Use JavaScript Variables in Functional Scope
A variable declared inside a function using these keywords is not accessible outside the function. That’s the applied functional scope.
It is true irrespective of whether you use var, let, or const. Inside the function, they are pretty similar in managing a variable’s scope.
None of the variables are accessible outside of the function, not even age which is declared using var. So the variable declared with var inside a function is not accessible outside of it. The keyword var has a function-scope.
How to Use JavaScript Variables in Global Scope
Variables declared outside of any functions and blocks are global and are said to have Global Scope. This means you can access them from any part of the current JavaScript program.
You can use var, let, and const to declare global variables. But you shouldn’t do it too often.
The variables are accessible everywhere. To restrict the scope of a variable using the var, let, and const keywords, here’s the order of accessibility in scope starting with the lowest:
var: The functional scope levellet: The block scope levelconst: The block scope level
How to Reassign a New Value to a Variable in JavaScript
Once you’ve declared a variable with var or let, you can reassign a new value to the variable in your programming flow. It is possible if the variable is accessible to assign a value. But with const, you can’t reassign a new value at all.
There is a tricky part with const that you must be aware of. When an object is declared and assigned a value with const, you can still change the value of its properties. But you can not reassign another object value to the same variable. This is a common mistake many devs make.
What Happens When You Access a Variable Before Declaring it in JavaScript
As a pragmatic programmer, you should never try accessing a variable without declaring it. But in case it happens, let’s see how the variable may behave.
With var in non-strict mode, the variable will have a undefined value. This means that a variable has been declared but has no value assigned.
In strict mode, you will get a ReferenceError that the variable is not declared. With let and const, if you try to access a variable before declaring, you will always get a ReferenceError.
You need to consider these circumstances and concepts to evaluate how var, let, and const behave. So, the rule goes:
- Don’t use
varanymore. - Use
letorconst. - Use
constmore often. Useletwhen you need to reassign another value to a variable. - Don’t try to access a variable without declaring it.