Expressions vs. Statements

Expression - a snippet of code that evaluates to a value.

Statement - a snippet of code that performs an action.

Expressions

Primary Expression - a single value

Primary expressions refer to stand alone expressions such as literal values, certain keywords and variable values.

let total = 0;

'hello world'; // A string literal
23;            // A numeric literal
true;          // Boolean value true
total;         // Value of variable total
this;          // A keyword that evaluates to the current object
add(1,2);      // A function call result

Arithmetic Expression - an arithmetic operation that evaluates to a numeric value

// 10 + 3 is evaluated by the JS Engine to return the value 13

> 10 + 3;
13


// 10 * 3 is evaluated by the JS Engine to return the value 30
// which is then passed as the argument to the console.log method

console.log(10*3); // 30

String Expressions - evaluate to a string

Logical Expressions - evaluate to true of false (boolean values)

Left-hand Side Expressions - anything that can be assigned a value

Any variable that can be assigned a value can be evaluated as an expression

Assignment Expression

The value of an assignment expression is the value of the right-side operand. As a side effect, the = operator assigns the value on the right side to the value on the left side.

The result of a function call can be used in an assignment expression.

Statements

A statement is an instruction to perform a specific action. There are two types of statements.

Declarative

Includes statements for creating a variable or a function.

Expression Statements

Wherever you can use a statement, you can write an expression.

The reverse is not allowed. You cannot use a statement in place of an expression.

Conditional Statements

Conditional statements execute statements based on the value of an expression. Examples of conditional statements includes the if/else and switch statements.

Last updated

Was this helpful?