Variables in JavaScript

0

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:

There are several ways to declare variables across all programming languages. In javascript, if you want to declare a variable, you must first provide the variable type, then the variable name, then the equals sign (=), and finally, you have to give a value.

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..



The usage of Const prevents value from being re-assigned, hence running this code will result in the following error.


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:


Here I have assigned the value of the variable at the end so, in the output Age is Undefined.


Variable Name: 
You can give the Variable Name whatever you want but in this case, you need to keep these things in mind:
1. There are some reserved keywords in JavaScript, they cannot be used as variable names.
2. Variable Name must be in one word, space cannot be given in between.
3. Quotation cannot be used in Variable Name.
4. Variable Name cannot start with a number, eg: 50Name, but can be used at the end of the name. For example: Name50.
5. Hyphens cannot be used between variable names, eg: My-Name, but underscore can be used. For example: My_Name.

Value: At the end of all you give the Value of the variable.

Hope you understand these things. Thank you.

Post a Comment

0Comments
Post a Comment (0)