The concept of variables is primary to most programming languages. There are hardly any high language that does not deal with variables. This is so because, variables are an important concept of algebra.
Consider the following mathematical equations -
Trivially we can see that x and y might be 3 and 2, but if we had to formally solve this, we can proceed so forth.
Now using the resolved value of x,
3 + y = 5
=> y = 2
During the entire process, what you possibly realised is that, what x really is, is a container / wrapper for the integer 3.
In computer programming, variables work similarly.
The way to declare and store data in variables, and use them is as follows -
var a = 10;
var b = 11;
var c = b - a;
console.log(c); // Output: 1
{% hint style='working' %}
NOTE the usage of the operator =
here. It works as 'assignment' operator in Javascript (and most other programming langauges). var a = 10
means, in english - "Let the value of a be 10", and not the statement "The variable a is equal to 10"
{% endhint %}
The mutability of a variable says whether we can change the value of a variable once it has been declared. There are 3 variable declaration keywords - var
, let
and const
And this how var
and let
work.
var a = 10;
a = 12; //value can be changed
let b = 20;
b = 0; // value can be changed
It may appear to not be any different, but var
and let
have different scopes (we are soon going to cover that).
And this is how const
works -
const k = 123;
k = 0; // Error: Because we cannot change value of const.
When we create a variable, we have a LHS (Left Hand Side) and a RHS (Right Hand Side). The LHS is the name of the variable, and the RHS is the data contained in the variable. (In computer science, those are technically called l-value and r-value in fact).
Whenever we write something like myVar = 10
, we are reassigning the LHS to point to a new RHS.
In Javascript the assignment statements are value-copy and not reference-copy type.
var a = 10;
var b = a;
b++;
console.log(b); // 11
console.log(a); // 10 (not 11)
In line no. 2 in the snippet above, b = a
, here's what happens : -
- Interpreter looks at RHS. Finds
a
- Interpreter finds out the value of
a
(i.e. 10) - Interpreter assigns this value to the LHS, which is
b
- Now
b
contains the value10
, anda
contains the value10
- At this point, after execution of this line, there is no relationship between b and a anymore.
{% hint style='danger' %}
This behaviour holds true only for variables that have data like number
, string
or boolean
. For other data types like objects and arrays, assignments are reference-copy, and not value-copy. We will see this later
{% endhint %}