Expressions vs Statements

Published on

Last updated on

1 min read--- views

JavaScript distinguishes expressions and statements. A statement is an instruction, an action.

Remember conditions with if, loops with while and for — all those are statements, because they just perform actions and control actions, but don't become values.

Expression returns (expresses) a value. Each of the following lines contains an expression:

100 // this is a literal that expresses number 100 getUserName() // expresses Serhii 5 + 2 // expresses 7

Statements produce or control actions, but do not turn into values:

let x; // declare a variable 'x' function foo() {} // declare a function 'foo' function bar() { return null // return is also a statement }

You can't put statements where expressions are expected. For example, passing a const statement as a function argument will produce an error. Or trying to assign the if statement to a variable:

let b = if (x > 10) { return 100; }; // error! console.log(const y); // error!

Share it: