Variables in JavaScript

Like a jar

We can treat JavaScript variables like labeled jars. You can understand this by the diagram where we have a value and it goes into the sort of a container that has a label on it. In this case, we are saying age is 72. S, in the variable we can store value; give it a name so that we can recall them later.

Variables are a very important concept in programming. It’s not important in JavaScript but also in all other programming languages.

Basic Syntax

Basic Syntax

The basic syntax that we will see in first is with the keyword called “let”. Now there are actually two different ways to writing variables in JavaScript. These two are commonly used. These are let and const.

READ MORE: JavaScript Interview Questions and Answers

Variables in JavaScript with Let Keyword

So, let is the keyword we start with and then a variable name, you can name it anything you want. The name could be my variable or stupid variable and then an equal sign (=) and at last the value of a variable. In JavaScript we can write variable like:

let age = 72;

The let keyword is representing that it is a variable and the age is the name of the variable, at last, we have number 72 after the equal sign (=) that is the value of the variable age. You can recall this age variable at any point in your JavaScript program by typing its name age.

Recall Variables

In the above example, we have two variables “hens” which have a value of four and “roosters” which has a value of two. We added them together by recalling them again. We are not recalling them here but also give a nice name. A way of understanding what it is? We can see clearly we are adding hens to roosters and trying to calculate the total number of chickens.

The main reasons for variables are here that we are storing information so, that recall it and use it later on. So, hens + roosters give us the result 6.

let hens = 4;
let roosters = 2;
hens + roosters      //6

Update Values of Variables

Update Values of Variables

In the above figure, I have four hens but a raccoon killed a hen which is very sad. So, we have to remove the killed one hen from our variable “hens”.  You can see in the above figure that remove the 1 hen from the variable “hens” by using the negative sign (-) but it not changed the value of variables “hens”.

So, we can update the value of “hens” variable like this:

let hens = 4;
hens – 1;                      // this does not change the value of variable hens.
hens = hens – 1;         // this will change the value of hens variable to 3.
hens                           // 3 Now the hens variable has value of 3.

In the above figure, we just updated the value of the “hens” variable by setting the “hens” variable equal to the variables current value which is four, and then we subtracted one from it by using the negative sign.

If you like this article on Variables in JavaScript. Please share it with your friends on Facebook, Twitter, etc.

Muzammal Ahmad

I am an Ethical Hacker, Programmer, and SEO (Search Engine Optimization) expert. I love to write blogs and share knowledge with people. I also like to help people to solve their technical or internet relates problems.

Leave a Reply

Your email address will not be published. Required fields are marked *