Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Conditional Branching in n8n with the If Node

Tech 1

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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.