If#
The If node is used to split a workflow conditionally based on comparison operations.
Node Reference#
Add comparison conditions using the Add Condition dropdown. The available comparison operations vary for each data type.
Boolean
- Equal
- Not Equal
Number
- Smaller
- Smaller Equal
- Equal
- Not Equal
- Larger
- Larger Equal
- Is Empty
String
- Contains
- Equal
- Not Contains
- Not Equal
- Regex
- Is Empty
You can choose to split a workflow when any of the specified conditions are met, or only when all the specified conditions are met using the options in the Combine dropdown list.
Branch execution with If and Merge nodes#
0.236.0 and below
n8n removed this execution behavior in version 1.0. This section applies to workflows using the v0 (legacy) workflow execution order. By default, this is all workflows built before version 1.0. You can change the execution order in your workflow settings.
If you add a Merge node to a workflow containing an If node, it can result in both output branches of the If node executing.
The Merge node is triggered by one branch, then goes and executes the other branch.
For example, in the screenshot below there's a workflow containing a Set node, If node, and Merge node. The standard If node behavior is to execute one branch (in the screenshot, this is the true output). However, due to the Merge node, both branches execute, despite the If node not sending any data down the false branch.
Example Usage#
This workflow executes two different Set nodes based on the output given by an IF node. You can also find the workflow on n8n.io. This example usage workflow would use the following nodes. - Start - Code - IF - Set
The final workflow should look like the following image.
1. Start node#
The start node exists by default when you create a new workflow.
2. Code node#
- Enter the following code:
1 2 3 4 5 6 7 8 9 10 11 12
return [ { json: { id: 0, } }, { json: { id: 1, } } ];
- Click on Execute Node to run the workflow.
3. IF node#
- Click on the Add Condition button and select 'Number' from the dropdown list.
- Click on the gears icon next to the Value 1 field and click on Add Expression.
- Select the following in the Variable Selector section: Nodes > Function > Output Data > JSON > ID. You can also add the following expression:
{{$node["Function"].json["id"]}}
. - From the Operation dropdown list, select 'Equal'.
- Click on Execute Node to run the workflow.
4. Set node (for 'true' condition)#
- Create a Set node connected to the 'true' output of the IF node.
- Click on the Add Value button and select 'String' from the dropdown list.
- Enter
name
in the Name field. - Enter
n8n
in the Value field. - Click on Execute Node to run the workflow.
Note: Notice that only the ID with the value 0 made its way to this Set node.
5. Set1 node (for 'false' condition)#
- Create a Set node connected to the 'false' output of the IF node.
- Click on the Add Value button and select 'String' from the dropdown list.
- Enter
name
in the Name field. - Enter
nodemation
in the Value field. - Click on Execute Node to run the workflow.
Note: Notice that only the ID with the value 1 made its way to this Set node.