In this section we’re assuming you’re already familiar with the variables in general.
In this section we will explain how we can declare and initialize a variable with the var keyword.
In JavaScript, to create a variable, we use the var keyword before the name of the variable. This will signal the execution engine that the target name is actually a variable. So the engine will set aside a portion of the memory for that name and link the name and the memory space to each other.
Example:
var variable_name;
`variable_name`: This is the name of the variable that we want to create. Could be any name as long as it follows the rules of naming in JavaScript and it's not a reserved keyword in the JavaScript.
Semicolon ;: The semicolon is essentially used to define an statement. In JavaScript, using semicolon is optional as long as there’s only one statement in each line.
For example if we had two statements in one line, we then need to use the semicolon to separate them:
var age; const Pi;
Here we’ve created a variable named age and a constant identifier named Pi. So because these two statements are independent from each other, we need to use the semicolon in between.
Note: the use of semicolon after the const Pi statement is optional.
We can also initialize variables when we declare them at the same time.
Example:
var age = 30;
We can declare multiple variables in a single statement as well:
var variable_one, variable_two, variable_n;
Note: make sure to separate each variable from the other via the colon ,.
Note: we could also assign a value directly to each variable if we wanted.
For example:
var variable_one = 10, variable_two = 30, variable_n = 50;
Example:
<!DOCTYPE html>
<html>
<head>
<title>JS is fun :)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h3>Output:</h3>
<p id = "link">Hi</p>
<script>
var age = 30, height = 200;
document.getElementById("link").innerText = `The age is ${age} and the heigth is ${height}`;
</script>
</body>
</html>
In JavaScript we declare a variable only once and after that, we only assign values to the variable or get the value from that variable. Basically there’s no need to declare a variable every time we need it.
Example:
var age = 20;
age = 40;
age = 50;
console.log(age);
Here we’ve declared a variable with the name age. The initial value of this variable is set to 20.
In the next line, we’ve called the variable and assigned another value for it to store. This means the current value in the variable will be replaced with this new value (40).
After that, we’ve called the variable again and assigned another value to the variable which is 50. This means the current value of the variable (40) is replaced with the value 50.
At the end we’ve called the console.log(age) statement. As mentioned in the variables section, when a variable is not used on the left side of the assignment = operator, it will act as the giver. This means it will return the value that is stored in its memory space.
So here the current value of the age variable is returned (50) and sent to the browser’s console.