Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An expression statement is a JavaScript statement that consists entirely of a single expression. When the JavaScript engine encounters an expression statement, it evaluates the expression to compute its value, updates the completion record, and subsequently discards the resulting value before advancing to the next statement in the execution context.
Expression ;

Abstract Syntax Tree (AST) Representation

In the ECMAScript specification, this is represented by the ExpressionStatement node. The parser wraps the underlying Expression node to elevate it to the statement level, allowing expressions to exist in contexts where the grammar strictly requires a statement (e.g., within a Block).

Lookahead Restrictions

To prevent syntactic ambiguity during parsing, the ECMAScript specification enforces strict lookahead restrictions on expression statements. An expression statement cannot begin with specific tokens that would cause the parser to interpret the construct as a declaration or a block. The formal grammar defines this as: ExpressionStatement : [lookahead ∉ { {, function, async [no LineTerminator here] function, class, let [ }] Expression ; If an expression statement were to start with any of the restricted tokens, the parser would misinterpret the intent:
  1. { (Left Brace): Parsed as a Block statement rather than an ObjectExpression.
  2. function: Parsed as a FunctionDeclaration rather than a FunctionExpression.
  3. class: Parsed as a ClassDeclaration rather than a ClassExpression.
  4. let [: Parsed as a LexicalDeclaration (destructuring) rather than a property access on a variable named let.
  5. async function: Parsed as an AsyncFunctionDeclaration.

Syntax Visualization and Disambiguation

To force the parser to treat restricted starting tokens as part of an expression statement, the expression must be wrapped in a Grouping Operator ().
// Variable initialization to prevent ReferenceErrors during execution
let y = 10, z = 20, x = 0;

// Valid Expression Statements
x = y + z;
x++;
"use strict";

// Ambiguity: Parsed as a Block Statement containing a labeled statement 'a'
{ a: 1 }; 

// Disambiguation: Grouping operator forces ObjectExpression parsing
({ a: 1 });

// Ambiguity: Parsed as an invalid FunctionDeclaration 
// (Commented out because it throws a SyntaxError: Function statements require a function name)
// function() { return true; }(); 

// Disambiguation: Grouping operator forces FunctionExpression parsing (IIFE)
(function() { return true; })();

// Ambiguity: Parsed as a LexicalDeclaration
let [b] = [1];

// Disambiguation: Grouping operator forces MemberExpression parsing on identifier 'let'.
// (Commented out because 'let' cannot be used as a variable identifier in strict mode, 
// and would throw a ReferenceError if undeclared in non-strict mode)
// (let)[b] = [1]; 

Completion Values

While the evaluated value of an expression statement is discarded during standard script execution, it is stored in the execution context’s Completion Record. This internal value is not accessible programmatically within the script itself, but it is returned by the eval() function and is the value printed by Read-Eval-Print Loop (REPL) environments (like browser consoles) after executing the statement.
// The completion record of this expression statement is 42
eval("40 + 2;"); // Returns 42
Master JavaScript with Deep Grasping Methodology!Learn More