With the help of programming language Javascript, programmer creates the dynamic, robust and responsive online applications. To store and update the data via variables is basic fundamental approach in Javascript. In Javascript there are three ways to declare variables using let, const and var. To write and understand the clean and manageable code, it is very important to understand these three keywords are different from one another. Each of these keywords has unique identity and scope. Let’s understand more about these keywords in this post.
let :
let keyword plays important role in Javascript and it was introduced in ECMAScript 6 a.k.a ES6. It declares in block-scoped and limited to the context. The special feature of let for preventing the unintentional variable hoisting and reduce the possibility of scope-related errors.
Refer the below example of let :
function usingLet() {
if(1==1) {
let num = 1;
}
console.log(num); // ReferenceError : num is not defined
}
usingLet();
In the above example, num is declared using let keyword inside the if block, and it is not accessible outside the if block. Trying to access num outside the if block will throw the ReferenceError.
const :
Const, this keyword introduced in ES6, and has unique feature of variables that cannot change their initial value. Const is also a block-scoped like let and hoisted same as let. const is useful whenever declaring variables that value shouldn’t change while the execution of code. Also, one more important feature of const is cannot declare the variable without initial value assignment.
Refer the below example of const :
function usingConst() {
const num = 10;
num = 20; // TypeError: Assignment to constant variable
}
usingConst();
In the above example, num is declared using const and immediately assigned the value 10. When we attempt to reassign it to 20, a TypeError is thrown as const variable cannot be reassigned.
var :
Since the Javascript introduced, var has been the most traditional method of declaring variables. Var-declared variables are function-scoped, which means their scope is limited to the function in which they are specified. The var variables can be accessed inside of a block, such as a loop or conditional expression not like let and const.
Refer the below example of var:
function usingVar() {
if(1==1) {
let num = 1;
}
console.log(num);
}
usingVar();
In the above example, num is declared using var keyword inside the if block, but still accessible outside the block.
Use let and const instead of var when writing Javascript code it will help to prevent the unexpected scope problems and create more predictable, maintainable and cleaner code. If you need to reassign a variable, use let; if you wish to declare constants, use const.
5 Comments
Vaishali you made a syntax error at var example as instead of var you wrote let which means its block scoped and won’t be accessible outside of that block.
1
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your article helped me a lot, is there any more related content? Thanks!
viagra kroger benadryl cream The International Monetary Fund and some U where can i buy priligy in usa Stock flasks were split on a weekly basis at a 1 4, 1 10, or 1 20 ratio