Implementing Conditional Branching in n8n with the If Node
The If node serves as the primary control flow mechanism in n8n, enabling dynamic path selection based on data conditions. Items matching specified criteria flow through the upper true output, while non-matchnig items route through the lower false output.
Configuration Parameters
Four core elements define conditional logic:
Data Types Select the approrpiate scalar type for comparison:
- String: Text matching (exact, contains, regex)
- Number: Mathematical comparisons (equalities, ranges)
- Boolean: True/false state verification
- Date/Time: Temporal comparisons (before, after, between)
Comparison Logic Each condition requires three components:
- Left Operand: Dynamic data reference from upstream nodes (expression)
- Operator: Evaluation method (Equals, Contains, Greater Than, Starts With)
- Right Operand: Static value or secondary dynamic reference
Composite Conditions Chain multiple evaluations using:
- AND: All conditions must satisfy simultaneously
- OR: Any single condition satisfies the requirement
Output Routing The node evaluates each input item independently, partitioning the dataset into two streams for parallel processing.
Practical Example: Decision Game
Consider a workflow where users submit a choice via form, and the system randomly generates an opposing choice to determine the outcome.
First, generate the machine's selection:
const options = ["rock", "paper", "scissors"];
const selection = options[Math.floor(Math.random() * options.length)];
return { machineChoice: selection };
Next, an If node checks for a draw by comparing the user's submission against the generated value:
- Left Value:
$json.userInput - Operator:
Equal - Right Value:
$json.machineChoice
If false (no draw), subsequent If nodes evaluate game rules. When the user selects "rock", another If node checks if the machine chose "scissors" (win) versus "paper" (loss), routing to appropriate handlers.
Production Application
In inventory management scenarios, the If node filters API responses:
Condition: $json.stockLevel < $json.reorderThreshold
True Branch: Trigger notification workflow
False Branch: Update database timestamp
Optimization Strategies
Null Safety
When evaluating optional fields, precede value comparisons with an Exists or Is Not Empty check to prevent runtime errors on undefined properties.
Multi-Path Routing For scenarios requiring more than binary outcomes (e.g., priority levels: critical, warning, normal), utilize the Switch node instead of nested If nodes. Switch supports multiple discrete output channels based on value matching, reducing workflow complexity.