Javascript Variable

This is the second part of the Basic Javascript Series. We will discuss variables in this article. You store water in a bucket. Likewise in programming language variables are the container where you can store some value. There are different keywords by which we can declare variables. var, let and const are the three keywords by which we can declare variables.

  • The var keyword was used in the old version of javascript from 1995 to 2015.
  • The let and const keywords were used in the modern Javascript version after 2015.
  • The var keyword should not be used for the New javascript browser. You should only use the var keyword for old javascript browsers where the let and const keyword is not supported. See the support for the var, let, and const keywords.

Here we will only discuss the use case of let and const because you don’t need to use the var keyword.

So the first step is to declare the variable. After that, you will assign a value to that variable. For example, you need to do a summation of two numbers. If you think of it programmatically you first have to think of the two numbers that you want to add up. Then you will do the summation operation. Let me show you how you can do it programmatically.

The first number will be 5 and the second number will be 7. We will assign each of these values to a variable x and y respectively. how do we do that?

let x = 5;
let y = 7;

In your VS Code write or copy-paste these two lines.

Now you want to print the value of x and y. You will use console.log() method to do it. Now our program looks like this.

let x = 5;
let y = 7;
console.log(x);
console.log(y);

Run the program from the terminal by typing node index.js. Here index.js is the file where we are working on. The output will be like this

node index.js
5
7

Now we will do the addition of these two numbers. We will declare another variable to store the summation value of these two numbers.

There are conventions in the javascript community for declaring variables. Where you have to put words by capitalizing the first letter of the word. What do I mean by this? Let me declare a variable: addXY.

let addXY = x + y

So the complete program is looking like this.

let x = 5;
let y = 7;
console.log(x);
console.log(y);
let addXY = x + y

Now add console.log(addXY) to the last line of the program. The program will now look like this.

let x = 5;
let y = 7;
console.log(x);
console.log(y);
let addXY = x + y;
console.log(addXY);

In the terminal type node index.js and press enter. The output will be like this.

5
7
12

Do a couple of exercises like addition, subtraction, multiplication, and remainder. As you practice you will be master of it.

Now see the following code.

let x = 5;
console.log(x);
x = 10;
console.log(x);

Here first we have defined x and assigned a value of 5 to x. We are printing the value. Then we are reassigning 10 to x. Again we are printing the value of x. Notice here it is possible to reassign the value of x with another value that is 10. Replace now let with const.

const x = 5;
console.log(x);
x = 10;
console.log(x);

Run the program by typing the node index.

TypeError: Assignment to constant variable.

So if we use the const keyword we can not reassign a new value to x. If we use the let keyword we can reassign a value to x.

This is the difference between the let and the const keyword. Now you know where to use the let and where to use the const. Generally, it is good to use const before declaring a variable. In many cases, although we need to use let as well.

In the next article, we will see JavaScript Operators.

Leave a Comment