A series of instructions that can be tell a computer to do stuff is code. I am all against text-book definitions. The ability to build stuff out of thin air is perhaps a superpower and programming/coding enables us to do just that.
A statement is a line of code that tells computer to do a specific task.
x = y + 5;
Now we can see that we have used letters, x and y. These are variables. Think of variables like buckets where you can store a value (Same as the ones we use in algebra).
The number 5 is quite literally 5. Its called a literal value ( No pun intended.) The = and + symbols are operators. + is used for addition and = is the assignment operator.
The statement simply adds the value 5 to whatever the value of y is and assigns it to x.
A group of expressions make a statement.
a = b * 2;
Here there are 4 expressions here,
- 2 is the literal value expression.
- b is the variable expression.
- b * 2 is the arithmetic expression.
- a = b * 2 is the assignment expression.
Please note that b * 2 is also an expression and can independently be a statement at the same time.
There is also something called a call expression which we will encounter in further posts.
This is a common interview question and one that is widely misunderstood. You'll see that many blogs and videos on the internet talk about JS being an interpreted language. Before we look into that let's talk about interpreters and compliers, just to give some context for budding programmers.
As seen above
x = y + 5 is easily understood by a human. But underneath the hood the computer translates the code to what it can understand. Some computer languages run the code top to bottom, line by line - this is called 'Interpreting' the code.
There are other computer languages where the translation is done ahead of time. Hence, when the program runs, it actually runs the compiled code.
Type the below code in the console (Or use jsfiddle):
a = 5;
b = a * 4;
- Assign the literal value 5 to the variable a.
- Now multiply the literal value 4 to whatever the value of variable a is (in this instance, 5) and assign the product to the variable b.
- Print the value of b. (We will be discussing console.log() later)
You will get an output 20.