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.
Arithmetic Expression - an arithmetic operation that evaluates to a numeric value
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?