From a Gitbook I have started writing
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.
2.2.1 Declaring and Using Variables
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;
NOTE the usage of the operator
var a = 10 means, in english - "Let the value of a be 10", and not the statement "The variable a is equal to 10"
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 —
And this how
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
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.
var a = 10;
var b = a;
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
- Interpreter finds out the value of
- Interpreter assigns this value to the LHS, which is
bcontains the value
acontains the value
- At this point, after execution of this line, there is no relationship between b and a anymore.
This behaviour holds true only for variables that have data like
boolean. For other data types like objects and arrays, assignments are reference-copy, and not value-copy. We will see this later
Originally published at coding-blocks.gitbooks.io.