Variable is a very important topic for all programming languages, not only JavaScript. Therefore, you need to have a solid understanding of variables regardless of the programming language you choose. Everything must be done using variables as you advance. So I hope you take the time to fully understand what we're discussing today.
Let's imagine you develop an application with your information (name, institute name, and age), and this data is in many places. As seen in the figure below, you inserted them there.
Next year, when you're older and switch institutes, you'll need to update each of these data individually. It is tedious and time-consuming to hold and modify 1000 lines of code if the data is present in 1000 different locations. Variables are the solution to this problem.
You can compare variables to a container. A variable is a container that holds your data.
If you put your name, institute name, and age in a variable, you don't have to go everywhere and change the code again and again. If you only change the values of the variable, the value of all the places where the variable is present will change. To print a variable, the name of the variable in console.log( ) will be printed, it cannot be placed inside a single quotation or double quotation, and if you want to add a String or text before/after the variable, then it (+) must have to be added before/after the String. Run the following code on your computer….
When you run this code, you'll see an output similar to this. Remember that changing the variable's value will also change your output. Now try again by yourself using your name and age.
Variable Declaration in JavaScript:
Variable_Type Variable_Name = Value;
Example:
var Name = "Ahnaf Rashid";
Variable Type: The three keywords below must be used as the variable type when declaring a variable in JavaScript:
1. Var: If we use the var keyword, we can later re-assign and re-declare the variable. Re-assign means to change the Value of the Variable and Re-declare means to re-declare the Variable. In other words, changing a variable's value after assigning it is known as re-assigning, and re-declaring the variable in any part of the code is called re-declare.
2. Let: If we use the let keyword, we can later re-assign the variable but we cannot re-declare it.
3. Const: If you use the const keyword, you cannot re-assign or re-declare the variable later.
Let's run the following code now, in this code I have tried to re-assign the Value of 3 types of Variable Type..
Again, if I attempt to re-declare the variable, this error will appear since re-declaring the variable using Const and Let is not possible.
An important thing to remember is that JavaScript code is executed line by line, so the value of the variable must be assigned before the code execute. See the following code:
Variable Name: You can give the Variable Name whatever you want but in this case, you need to keep these things in mind:









