Master Conditional Logic in n8n - If-Else Node, Execution Order & Branching

Master conditional logic in n8n with the If node. Learn execution order, branching, and when to use Filter vs If nodes for dynamic workflows.

Table of Contents

Overview

In this tutorial, you will learn how to master conditional logic in n8n using the If node, understand execution order, and implement branching strategies to build intelligent, adaptable workflows. By the end, you'll be able to create workflows that handle different scenarios dynamically, manage multiple branches efficiently, and combine data streams seamlessly using the Merge node.


Understanding Conditional Logic in n8n

Conditional logic is the backbone of smart workflows. It lets you ask questions like "If this condition is true, do X; otherwise, do Y." This approach is essential for workflows that need to handle different data inputs or outcomes dynamically.

Key Concepts of Conditional Logic

  • If-Else Condition: The most common form of conditional logic where the workflow splits into two branches — one for true (if the condition is met) and another for false.
  • Examples:
    • If a ticket is marked urgent, notify the manager; else, assign normally.
    • If a customer opens an email, send a follow-up; else, add them to a different sequence.
    • If a lead is from a high-priority industry, fast-track them; else, assign to the normal pipeline.

Implementing If-Else Logic with the If Node

The If node in n8n allows you to define one or more conditions that split the workflow into two branches:

  • True Branch: Executes when the condition(s) are met.
  • False Branch: Executes when the condition(s) are not met.

Example Conditions in an If Node

{
  "conditions": {
    "all": [
      {
        "field": "companyName",
        "operation": "isNotEmpty"
      },
      {
        "field": "email",
        "operation": "doesNotEndWith",
        "value": "gmail.com"
      },
      {
        "field": "email",
        "operation": "doesNotEndWith",
        "value": "hotmail.com"
      }
    ]
  }
}

These conditions check that the company name exists and the email domain is neither Gmail nor Hotmail.

To better understand how data flows and transforms through nodes like If, consider reviewing Understanding Data in n8n, which explains how data is structured and passed between nodes.

Using the Filter Node as an Alternative

The Filter node is another way to apply conditional logic but differs because it does not output multiple branches. Instead, it filters out items that do not meet the condition, passing only the matching items downstream.

  • Kept Branch: Items that meet the condition.
  • Discarded Branch: Items that do not meet the condition (not forwarded).

When to Use Filter vs If

Feature If Node Filter Node
Outputs multiple branches Yes (True and False branches) No (only one output branch)
Use case When you need to perform different actions based on condition When you want to filter data and only continue with matching items
Workflow complexity Higher (due to branching) Lower

Practical Steps to Configure an If Node

  1. Drag the If node onto the canvas.
  2. Connect the previous node (e.g., Google Sheets) to the If node.
  3. Define your conditions under the Conditions section.
  4. Connect the True output to nodes that handle the true scenario (e.g., send Slack message).
  5. Connect the False output to nodes that handle the false scenario (e.g., update Google Sheets for unqualified leads).

For more details on configuring nodes that manipulate data, the official n8n Set node documentation provides useful insights, especially since expressions often complement conditional logic.


Branching in n8n: Types and Use Cases

Branching allows workflows to split into multiple paths based on conditions, enabling complex decision-making and parallel processing.

Types of Branching

  1. Conditional Branching (If Node): Splits workflow into two branches based on a boolean condition.
  2. Multi-Path Branching (Switch Node): Splits workflow into multiple branches (n branches) based on matching conditions.
  3. Parallel Branching: Executes multiple actions simultaneously from the same node output.

Multi-Path Branching with the Switch Node

The Switch node evaluates a single field against multiple values and routes the workflow into one or more branches accordingly.

Example: Branching Based on Order Status

Condition Branch Output Name
orderStatus == "pending" Pending Orders
orderStatus == "processing" Processing Orders
orderStatus == "cancelled" Cancelled Orders
orderStatus == "refunded" Refunded Orders

How to Configure the Switch Node

  1. Add the Switch node after your data source (e.g., order data).
  2. Select the field to evaluate (e.g., orderStatus).
  3. Add multiple values and assign branch names for each condition.
  4. Connect each branch to nodes performing the respective actions.

Parallel Branching

Parallel branching occurs when you connect multiple nodes directly from a single output, allowing simultaneous actions.

Example

  • From the Cancelled Orders branch, you send an email and send a Slack message concurrently.
  • From the Refunded Orders branch, you perform the same two actions in parallel.

Execution Order in n8n: How Branches Run

Understanding execution order is critical to avoid workflow errors, especially when branches depend on each other.

Default Execution Rules

  1. Branch Execution Happens Sequentially: One branch completes before the next begins.
  2. Top to Bottom Execution: Nodes higher on the canvas execute before nodes lower down.
  3. Left to Right Execution for Nodes at the Same Height: Leftmost nodes execute before rightmost nodes.

Why Execution Order Matters

If a node in one branch depends on data from another branch that runs later, your workflow will fail or produce unexpected results. Properly ordering branches and nodes ensures data dependencies are respected.


Merging Branches with the Merge Node

After splitting workflows into multiple branches, you often need to combine the results back into a single data stream for further processing.

Purpose of the Merge Node

  • Waits for all connected branches to complete.
  • Combines data from multiple branches into one dataset.
  • Supports different merge modes such as "Append", "Merge By Key", etc.

Practical Example: Merging Order Data

Suppose you have two data sources:

  • Orders Header: Basic order info like customer name, order date, status.
  • Order Details: Items within each order, quantities, prices.

You want to merge these datasets on the common orderID field.

Configuring the Merge Node

  1. Connect both data sources as inputs to the Merge node.
  2. Set the mode to Merge By Key.
  3. Select the key field to match on (e.g., orderID).

This will combine matching rows from both inputs into a single structured dataset.

For a deeper dive into the capabilities and configuration options, check out the Merge Node in n8n tutorial and the official n8n Merge node documentation.

Sample JSON Inputs and Merge Result

Input 1 (Orders Header):

[
  {
    "orderID": "1001",
    "customerName": "Alice",
    "orderStatus": "pending"
  },
  {
    "orderID": "1002",
    "customerName": "Bob",
    "orderStatus": "processing"
  }
]

Input 2 (Order Details):

[
  {
    "orderID": "1001",
    "product": "Widget A",
    "quantity": 2
  },
  {
    "orderID": "1001",
    "product": "Widget B",
    "quantity": 1
  },
  {
    "orderID": "1002",
    "product": "Widget C",
    "quantity": 5
  }
]

Merged Output:

[
  {
    "orderID": "1001",
    "customerName": "Alice",
    "orderStatus": "pending",
    "products": [
      { "product": "Widget A", "quantity": 2 },
      { "product": "Widget B", "quantity": 1 }
    ]
  },
  {
    "orderID": "1002",
    "customerName": "Bob",
    "orderStatus": "processing",
    "products": [
      { "product": "Widget C", "quantity": 5 }
    ]
  }
]

Step-by-Step: Building a Conditional Workflow with Branching and Merging

1. Set Up the Trigger and Initial Data Source

  • Use a trigger node like Webhook or Cron to start the workflow.
  • Add a data source node such as Google Sheets or HTTP Request to fetch relevant data.

2. Add Conditional Logic with the If Node

  • Drag the If node and connect it to the data source.
  • Define your conditions in the node.
  • Connect the True and False outputs to different nodes for respective processing.

3. Implement Multi-Path Branching with the Switch Node

  • Add a Switch node where multiple conditions need to be evaluated.
  • Configure values and output branches.
  • Connect each branch to the nodes that handle the specific path.

4. Use Parallel Branching for Multiple Actions

  • From any branch output, connect multiple nodes to execute parallel actions like sending emails and Slack messages simultaneously.

5. Merge Branches with the Merge Node

  • After processing branches, add a Merge node.
  • Connect the branches to the Merge node inputs.
  • Configure the merge mode and key fields as necessary.

6. Continue Workflow with Unified Data

  • Use the merged output for further processing, like aggregating data or sending notifications.

For tips on using expressions to dynamically set values in nodes like Set, which often work hand-in-hand with conditional logic, see Expressions in n8n.


Common Mistakes and Troubleshooting

  • Not configuring conditions correctly: Ensure your field names and operations in If or Switch nodes match the data structure exactly.
  • Ignoring execution order: Failing to arrange nodes properly can cause branches to execute in the wrong order, breaking data dependencies.
  • Forgetting to merge branches: If you split your workflow but don't merge branches that need to be combined, subsequent nodes may not receive the complete data.
  • Using If node when Filter is sufficient: Use Filter node when you only need to filter data without branching to simplify the workflow.
  • Mismatched keys in Merge node: When merging by key, ensure the key fields exist and are consistent in both inputs.


Quick Reference Cheat Sheet

Concept Node Used Description
Conditional Logic If Node Splits workflow into true/false branches
Filtering Data Filter Node Filters items passing only matching data forward
Multi-Path Branching Switch Node Routes workflow into multiple branches based on values
Parallel Branching Any node output Multiple nodes connected to same output for parallel actions
Merging Branches Merge Node Combines multiple branches into one dataset
Execution Order N/A Left to right, top to bottom; branches execute sequentially

By mastering these concepts—conditional logic, branching, execution order, and merging—you'll be able to build robust, flexible, and efficient n8n workflows that can adapt to complex real-world scenarios with ease. As shown in the video above, understanding and using these nodes effectively will elevate your automation skills significantly.

Frequently Asked Questions

The If node splits the workflow into two branches: the true branch executes when conditions are met, and the false branch executes when they are not.

Use the Filter node when you want to filter data and continue only with matching items without branching; use the If node when you need to perform different actions based on conditions with multiple branches.

Yes, the If node supports multiple conditions combined with logical operators like AND (all) or OR (any) to control workflow branching.

Execution order determines which branch runs first and how subsequent nodes process data, ensuring conditional logic flows correctly through true or false paths.

Examples include notifying a manager if a ticket is urgent, sending follow-ups if a customer opens an email, or fast-tracking leads from high-priority industries.

Dheeraj Sharma

Dheeraj Sharma

AI Systems Builder
Creator of the n8n Zero to Hero course (42 lessons, 31+ hours). I help solopreneurs build AI systems that grow revenue without growing workload.

Get the n8n Mastery Bundle

All workflows, cheat sheets, and premium resources from the entire course in one package.

Get Premium Resources