DEV Community

Cover image for Day 0 - JavaScript - Statements, Expressions
Geo Mukkath
Geo Mukkath

Posted on

Day 0 - JavaScript - Statements, Expressions

In the beginning there was code

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.

Statements

A statement is a line of code that tells computer to do a specific task.

In JavaScript, you might encounter such statements.

x = y + 5;

The above is a statement in JavaScript. Let's distill it down to the very basics.

Image description

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.

Expressions

A group of expressions make a statement.

a = b * 2;

Here there are 4 expressions here,

  1. 2 is the literal value expression.
  2. b is the variable expression.
  3. b * 2 is the arithmetic expression.
  4. 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.

Is JavaScript Compiled or Interpreted?(IMP!)

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.

JavaScript in reality uses a compiler, which goes against the popular notion that it is an interpreted languages. It compiles code and immediately runs the compiled code.

Starting coding with a basic statement

Type the below code in the console (Or use jsfiddle):

a = 5;
b = a * 4; 
console.log(b);
Enter fullscreen mode Exit fullscreen mode
  1. Assign the literal value 5 to the variable a.
  2. 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.
  3. Print the value of b. (We will be discussing console.log() later)

You will get an output 20.

Top comments (0)