DEV Community

Aditya Chakraborty
Aditya Chakraborty

Posted on

Expression vs. Statement

We programmers are in a lot of confusion between Expression and Statement. The purpose of this article is to clear this subject through a short overview.

The basic difference between an expression and a statement is that the expression returns something at the end of the day, produces data, and can be stored in one place. And the statement does not produce any data, cannot be stored anywhere, does not return anything. Defining a function is statement, and function call is expression. Because when you write a function, it does not return anything until the function is called. If the arrow function is written, it is an expression because it is being stored in a variable. Let's look at some examples.

const name1 = 'Rayhan'; // Statement
const name2 = 'Alvi'; // Statement
const name3 = 'Anik'; // Statement
const name4 = 'Arjun'; // Statement
const name5 = 'Ayman'; // Statement
Enter fullscreen mode Exit fullscreen mode

All of these are statements. Because they do not return anything.

const students = [
    'Rayhan',
    'Alvi',
    'Anik',
    'Arjun',
    'Ayman',
    'Ayuub',
    'Bidyut',
]; // Statement

console.log(students[0]); // Expression
console.log(students[1]); // Expression
console.log(students[2]); // Expression
console.log(students[3]); // Expression
console.log(students[4]); // Expression

for (let i = 0; i < students.length; i++) {
    console.log(students[i], students[i].toLowerCase()); // Expression
} // Statement
Enter fullscreen mode Exit fullscreen mode

students is statement, because it is not returning anything. console.log() is expression, because it returns something. for loop is statement, because it does not return anything, but console.log(students[i], students[i].toLowerCase()) inside the for loop is expression.

function nameOfFunction(name) {
    if (!name) {
        console.log('Please provide your name');
    } else {
        console.log('Hello', name);
    }
} // Statement
Enter fullscreen mode Exit fullscreen mode

This is called function statement. Because, it will not return anything until it is called.

const nameOfFunction = function (name) {
    if (!name) {
        console.log('Please provide your name');
    } else {
        console.log('Hello', name);
    }
} // Expression
Enter fullscreen mode Exit fullscreen mode

This is called function expression, because it is stored in a variable.

nameOfFunction('Murshed'); // Expression
nameOfFunction('Fahim'); // Expression
nameOfFunction(); // Expression
Enter fullscreen mode Exit fullscreen mode

Any function call is expression, because it returns something. If there is nothing to return it returns at least undefined.

function generateRandomNumber(min = 1, max) {
    const randomNumber = Math.floor(Math.random() * min + (max - min)); // Statement
    return randomNumber; // Expression
} // Statement

console.log(generateRandomNumber(5, 10)); // Expression
Enter fullscreen mode Exit fullscreen mode

This is the last example. Defining the function generateRandomNumber is statement, const randomNumber = Math.floor(Math.random() * min + (max - min)) is statement, Math.floor(Math.random() * min + (max - min)) is expression, return randomNumber is expression, console.log(generateRandomNumber(5, 10)) is expression.

I tried to give a short overview to clear the concept of expression and statement. Hope you can understand. 😊😊

Oldest comments (1)

Collapse
 
husienadel profile image
Husien Adel

Thanks a lot