JavaScript Expressions and Statements

Muhammad Hassan
3 min readApr 11, 2021
Parsing Expressions in JavaScript | by Chidume Nnamdi 🔥💻🎵🎮 | Bits and  Pieces

There are two major syntactic categories in Javascript:

  1. Expressions
  2. Statements

Expressions are parts of program code that can be combined and interpreted into a new value during processing. Together with statements, expression forms the building blocks of a program.

Expressions will be evaluated by the programming and returns a new value as a result of that evaluation. It is similar to arithmetical operation, in which we can do addition or subtraction between two numbers or more to create a new number as the result.

A statement performs an action. Loops and if statements are examples of statements. A program is basically a sequence of statements. Wherever JavaScript expects a statement, you can also write an expression. Such a statement is called an expression statement.

Expressions

Expressions are Javascript code snippets that result in a single value.

2 + 2 * 3 / 2

(Math.random() * (100 - 20)) + 20

functionCall()

window.history ? useHistory() : noHistoryFallback()

1+1, 2+2, 3+3

declaredVariable

true && functionCall()

true && declaredVariable

The above are expressions

Statements

Statements perform actions. In javascript, statements can never be used where a value is expected. So they cannot be used as function arguments, right-hand side of assignments, operators operand, return values…

foo(if () {return 2})

These are all javascript statements:

  1. if
  2. if-else
  3. while
  4. do-while
  5. for
  6. switch
  7. for-in
  8. with (deprecated)
  9. debugger
  10. variable declaration

Consider an example below and see what is an expression and what is a statement.

// first example
let name = "Nathan" + "Sebhastian"; // this is declaration of variable name
console.log(name);

// second example
let age = 10 + 6; // we declare variable age here
console.log (age);

// third example
function myFunction(a, b) { // here we declare a function with two parameters
if (a == "Johnny" && b == "English"){
console.log ("Welcome, Britain's greatest agent!");
}
else {
console.log ("Identification failed.");
}
}
myFunction ("Johnny", "English");

Time to identify expressions. Can you spot the part of our code that returns a value?

// first example
let name = "Nathan" + "Sebhastian"; // this is declaration of variable name with string concatenation expression
console.log(name);

// second example
let age = 10 + 6; // we declare variable age here with integer addition expression
console.log (age);

// third example
function myFunction(a, b) { // here we declare a function with two parameters, this is also a function definition expressions
if (a == "Johnny" && b == "English"){ // logical expressions comparing the values of a and b variables
console.log ("Welcome, Britain's greatest agent!");
}
else {
console.log ("Identification failed.");
}
}
myFunction ("Johnny", "English"); // this function call is an expression

Now let’s look at what part of the code is a statement.

// first example
let name = "Nathan" + "Sebhastian";
console.log(name); // statement

// second example
let age = 10 + 6;
console.log (age); // statement

// third example
function myFunction(a, b) {
// this entire if else block is a statement
if (a == "Johnny" && b == "English"){
console.log ("Welcome, Britain's greatest agent!");
}
else {
console.log ("Identification failed.");
}
}
myFunction ("Johnny", "English");

Entire sample code are statements

// first example
let name = "Nathan" + "Sebhastian"; // the code states that variable name's value is "Nathan" + "Sebhastian"
console.log(name); // statement

// second example
let age = 10 + 6; // this code states that variable age's value is 10+6
console.log (age); // statement

// third example
function myFunction(a, b) {
// this entire if else block is a statement
if (a == "Johnny" && b == "English"){
console.log ("Welcome, Britain's greatest agent!");
}
else {
console.log ("Identification failed.");
}
}
myFunction ("Johnny", "English"); // statement that contain function call expression

Statements can contain an expression. That kind of statement is called an expression statement.

Resources:

--

--