What is variable in JavaScript?

A variable in JavaScript is an identifier that provides a storage location used to store a data value. The data stored at a particular location in memory. That we accessed by using the variable name.
In JavaScript programs, a script will have to store the bits of information temporarily. It needs to do its work. It can store this data in variables. In other words, A variable is a symbolic name given to a location in the computer memory. Where we store the value, which can be used in the program. The value of the variable can change during the execution of the program. It may take different values at different times during the execution of the program.
The name of the variable that we choose must be meaningful to understand, what it represents in the program.

For example

var Price = 100;

Rules to follow:

  • The name of the variable can contain a letter, digits, underscore(_), and dollar sign($).
  • They must begin with a letter, not with a digit.
  • Uppercase and Lowercase are distinct. That means the variable Total is not the same as total or TOTAl.
  • It should not be a keyword.
  • White space does also not allow.
  • Variable can be any length.

Type of variable in JavaScript

There are two types of variables.

  1. Local Variable
  2. Global Variable

Local Variable: Local variables are variables that are defined within the function. The scope of this is local. This means you only can use the local variable within the functions that define them.

Global Variable: The global variables are that defined outside of the function. The scope of this variable is Global. That means you can use any function without passing them to the function as parameters.

JavaScript Variable example

var x = 10;  
var y = 20;  
var z=x+y;  

As a result:


let’s understand the JavaScript variable with Example.

So when we write JavaScript, we need to tell the interpreter every single step. That we need to perform. For this, sometimes we need to involve more details than expected.

Think about calculating the area of a wall. In math, the area of the rectangle is fined by multiplying two numbers. 

For example.

width x height = area

We can be able to calculate the area of the wall in our mind. But when we write a script to do this calculation, We need to give very detailed instructions to the computer. We tell the computer to perform some steps. As following order.

  • Remember the value of width
  • Remember the value of height
  • Multiply the width by height to get the area
  • Return the result to the user

It’s a bit complicated. In this care, we use variables to “remember” the values for width and height. We also can compare variables to short-term memory because once we leave the page, the browser will forget any information it holds.

Recommended posts

How to define constant in java

What is JavaScript?

Difference between the Java and JavaScript

Implementation of Array class in JavaScript

jQuery Introduction



Leave a Comment