Javascript Variable declaration (var, let, and const)
3 min readApr 29, 2021
In this, we will get to know var, let and const all this variable declarations in detail.
What are variables?
- Variables are the storage area in code used for storing a value in memory.
- In javascript Variables are created by using keywords such as var, let, const these are special reserved keywords in javascript.
- Javascript variables are loosely typed, Which means they can be stored in any data type such as number, string, boolean, array, Objects, null, undefined, and also functions
Important Note:
- In javascript variable name should always start with a letter ( a,b,c,…,z or A,B,C,…,Z ) , underscore line( _ ) or dollar sign ( $ ).
- Only after the first letter, we can use digits ( 0,1,2,3,…,9).Example
var Target_101 = value ;
- And most important javascript variable names are case sensitive, For Example ‘name’ and ‘Name’ are treated as two different variables.
1. Let’s take a look at Variable (var)
- Before ES6 was out we use to declare a variable in javascript using the keyword var.
- It can be of any data type such as string, boolean, array, objects.
- Have a look at the example below:
You can practice this code on https://jsfiddle.net/ please do try this on your own to be confident about it.
Note:
- A variable declared with var can be redeclared further in code with a new value.
- It can be declared globally, which means outside the function or class at the topmost level.
2. Now we dive into Variable (let)
- Keyword let helps us to declare local variables in block scope.
So, What is block ?
- Very line of code within the curly braces {} is a block.
- The scope of the variable declared with let is within that curly braces {}.
- This means the scope of the variable is within the block in which it is being used.
- Have a look at the example below:
In above code on line number 3 where name = “javascript”; will give us an error that name is already defined, this is because variables declared with let cannot be redeclared further in code.
- If the same variable name is declared within two different scopes then it will work as shown below:
Do try this code on https://jsfiddle.net/ so you can be confident about it.
Note:
- Declaring a variable with a let keyword helps to maintain the scope and makes our code much cleaner as well as organized.
- It can also be declared globally which means on the topmost level outside the function or class
3. Now the final Variable (const)
- So once we declare a variable using keyword const that variable name will maintain the constant values throughout the code.
- Even variables declared with const can only be accessed within the scope of a function or classes in which they are declared, But if we declare it globally it can be accessed by other functions or classes.
- Let’s look at the declaration below:
- However values within the declared variable cannot be reassigned, But we can add other values in its array or object further in code.
- This means it can gain some new values on the way if used with arrays or objects but old values can’t be redeclared For example:
Note:
- Even objects can be updated just by accessing them and assigning new values to them do try this on your own.
Reading someone else’s code is good But it’s best to practice the same,So we gain confidence about the topic.
Bonus
- For Cleaner code try using let and const variable
- It’s good to add comments at the variable scope so it becomes easy for you as well as other developers to know the scope of variables.
- Follow for more Javascript Basics with Siddhant.