Description

In programming, an expression is any valid combination of literals, variables, operators, and function calls that can be evaluated to produce a value. Expressions are fundamental building blocks of code — they are the “phrases” from which the “sentences” (statements and functions) of a program are constructed.

Every expression returns a value, even if that value is null, undefined, or a side effect (like a function returning void). Expressions can be simple (like 2 + 2) or complex (like Math.sqrt(x * y + z)), and they are evaluated according to the language’s operator precedence and associativity rules.

Examples of Expressions

Simple

5         // Literal expression
x         // Variable expression
x + y     // Arithmetic expression

Function Call

len("hello")    # Function expression that returns 5

Boolean Expression

age >= 18 && isVerified  // Evaluates to true or false

Nested Expressions

(result + 2) * (temp - 4)

Types of Expressions

TypeDescription
ArithmeticPerform mathematical operations (2 + 3, a * b)
LogicalEvaluate to boolean values (x > 5 && y < 10)
RelationalCompare values (x == y, x != z)
BitwiseOperate on binary digits (a & b, x << 1)
AssignmentAssign value to variable (x = 10, z += 5)
Function CallInvoke functions and return results (sqrt(x))
TernaryInline conditionals (x > y ? x : y)
Lambda / AnonymousExpression-based function definitions (x => x * x)
List/DictLiteral collection expressions ([1, 2, 3], {a: b})

Expressions vs Statements

FeatureExpressionStatement
Returns a valueYesNot necessarily
Can be part of another expressionYesNo
Executes logicSometimesYes

Examples:

// Expression
x + 5

// Statement
let total = x + 5;  // contains an expression but is itself a statement

In expression-oriented languages (e.g., Haskell, Lisp), even control flow constructs are expressions.

Expression in Different Languages

Python

a = 3 + 5  # Expression assigned to variable

JavaScript

const greeting = "Hello, " + name;

Java

int result = (a + b) * 2;

C++

int max = (x > y) ? x : y;  // Ternary expression

SQL

SELECT price * quantity AS total FROM orders;

Evaluation Rules

  • Operator Precedence: Determines which operation is performed first.
  • Associativity: Determines order when operators have the same precedence.
  • Short-circuiting: For logical expressions (&&, ||), evaluation may stop early.

Example:

true || (x++)  // x is not incremented because `true` short-circuits the evaluation

Expression Trees

An expression tree is a binary tree used to represent expressions, especially in compilers or calculators.

Example for 3 + (4 * 5):

    +
   / \
  3   *
     / \
    4   5

Used in:

  • ASTs (Abstract Syntax Trees)
  • LINQ queries in .NET
  • Symbolic computation (e.g., in Wolfram or SymPy)

Anonymous Functions as Expressions

Many modern languages allow anonymous functions (lambdas) as expressions:

Python

square = lambda x: x * x

JavaScript

const double = x => x * 2;

Expression vs Declaration

ConceptDescription
ExpressionEvaluated to produce a value
DeclarationIntroduces variables/functions into scope

Example:

let x = 10;  // Declaration + expression

Use in Functional Programming

Functional programming emphasizes expression-based code:

  • Functions return expressions
  • No side effects or statements
  • Control flow (if, match, etc.) is expression-based

Example (Scala):

val result = if (x > 0) "positive" else "non-positive"

Expression-Oriented Languages

Some languages treat everything as an expression, including control flow:

LanguageExpression-Oriented?
PythonPartially
JavaNo (mostly statement-based)
JavaScriptMixed
HaskellYes
ScalaYes
RustYes

Pitfalls and Gotchas

IssueCauseTip
Unexpected precedenceMisunderstanding operator precedenceUse parentheses for clarity
Type coercionLanguage auto-converts types unexpectedlyUse strict equality (e.g., ===)
Side effects in expressionsFunction calls with side effects in expressionsKeep expressions pure where possible
Mutating expressionsAssignments as expressions in some languagesAvoid in readability-critical code

Related Terms

  • Statement
  • Operator
  • Operand
  • Literal
  • Side Effect
  • Lambda Expression
  • AST (Abstract Syntax Tree)
  • Evaluation Order
  • Control Flow
  • Parser

Summary

An expression is any piece of code that returns a value when evaluated. From the simplest math calculation to complex nested function calls, expressions are the building blocks of all programming logic. Mastering expressions is essential for writing readable, efficient, and bug-free code across all modern programming languages.

Understanding how expressions behave — in terms of precedence, side effects, and data types — enables developers to write cleaner and more reliable software.