What is a VARIABLE in JavaScript?

Murodjon Tursunpulatov
2 min readMar 21, 2021

--

In this article, as usual, I am going to describe a variable in JavaScript.

A script will have to temporarily store the bits of information it needs to do its job. It can store this data in variables. When you write JavaScript, you have to tell the interpreter every individual step that you want it to perform. This sometimes involves more detail than you might expect. Think about calculating the area of a square, to find the area of the square we should multiply height to width.

area = height * width

You can calculate this area in your head easily but when writing a script you should give detailed instruction to the computer. You might write four steps like the below one.

  1. Remember the value of the width
  2. Remember the value of the height
  3. Multiply the width by height to get the area
  4. Console log result of the area

In this case, you would use variables to remember the values for width and height. The data stored in a variable can change each time a script runs.No matter what the dimensions of any square. You can find the area of a square multiplying the width by its height.

How to declare them?

Before you can use a variable you need to announce that you want to use it. This involves creating a variable and giving it a name. Programmers say that you declare the variable.

var area;

var is a keyword that the JavaScript interpreter knows that this keyword is used to create a variable. In order to use a variable you should give it a name in this case, area is the name of the variable.

Once you have created the variable, you can tell it what information you want to store. Programmers say that you assign a value to the variable.

area = 16;

area is the name of the variable. The equals sign (=) is an assignment operator that says you are going to assign a value to the variable. Until you have assigned a value to a variable, the value is undefined.

Using a variable to calculate the area of a square.

var width = 4;

var height = 4;

var area = height * width;

console.log(area);

You can use variables to store and kind of data types. Such as Numbers, Strings, Booleans, Arrays, Objects, Undefined and Null.

Conclusion

Variables are the essential part of coding without making variable we cannot achieve what we want. In all programming languages, we start learning the language from variables. If I give you something important, I am happy. Thanks for reading the article.

Resources used:

https://www.amazon.com/JavaScript-JQuery-Interactive-Front-End-Development/dp/1118531647

--

--

No responses yet