About this Article
In this short video, we’ll look at two of the n8n core nodes – the IF node and the Switch node.
These two nodes will allow you to split or branch your workflow in different directions:
- The IF node will allow you to split your workflow in two different directions, depending on whether a condition is True or False
- The Switch node will allow you to split your workflow in multiple directions, depending on the conditions you set inside the node
The n8n IF Node
The n8n IF node will allow you to branch your workflow based on whether the condition you set is true or false.
You can drag in data from previous nodes and have that matched against a value. You can either set the value yourself by typing it into the value 2 box, or you can match it against another expression from any other node, and one expression will be matched against another.
You can increase the complexity of the condition by adding further conditions to check. If you select AND, all conditions that you set must be matched in order to be true. If you select OR, then if any of the conditions that you set are matched, then the output will be true.
Looping With the IF Node
You can create loops using the IF node if some data you send to or pull into the workflow may change.
You can return the false branch back to a previous node and that will keep looping until the condition becomes true. You may want to use a wait node to stop this from looping back every second and using up resources.
If you want the loop to run only a number of times, you can add a code node into your loop. Inside of that cod node, you can use the function below:
// This function will count the number of loops by using the `$runIndex` variable
return {
loopCount: $runIndex + 1, // Increment the $runIndex to start count from 1
};
Then, add the output to another IF node with the condition that is greater than the number of loops you want this to run before it becomes true.
Copy the code below and paste to your canvas to use the loop workflow you see in the video:
{
"nodes": [
{
"parameters": {
"httpMethod": "POST",
"path": "test-order",
"options": {}
},
"type": "n8n-nodes-base.webhook",
"typeVersion": 2,
"position": [
-600,
-340
],
"id": "1d2d3358-1194-4320-b8b1-c0b6181458bf",
"name": "Webhook",
"webhookId": "2320fac0-cc0b-49f7-b67f-4a97a7e5d9fd"
},
{
"parameters": {
"url": "https://autom8this.com/wp-json/frm/v2/entries/634",
"authentication": "genericCredentialType",
"genericAuthType": "httpBasicAuth",
"options": {}
},
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-120,
-420
],
"id": "1e21f25b-fa4c-43ba-a2e8-c50e422688ab",
"name": "HTTP Request",
"credentials": {
"httpBasicAuth": {
"id": "eUn2NXajjrTcsMHU",
"name": "Unnamed credential"
}
}
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "02d1162a-81da-4aeb-9932-cce5ce5ae84c",
"leftValue": "={{ $json.body.order_add_newsletter }}",
"rightValue": "Yes",
"operator": {
"type": "string",
"operation": "equals",
"name": "filter.operator.equals"
}
}
],
"combinator": "or"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [
-380,
-340
],
"id": "0a5aa88b-e35a-4962-8f6f-5705adc30fc7",
"name": "If"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "a17f1e76-a612-4506-9481-9cac33209302",
"leftValue": "={{ $json.meta.order_payment_recd }}",
"rightValue": "Yes",
"operator": {
"type": "string",
"operation": "equals",
"name": "filter.operator.equals"
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [
100,
-420
],
"id": "a7d7bdbd-f855-4df0-81fe-6e1f98e99ae8",
"name": "If1"
},
{
"parameters": {
"amount": 3
},
"type": "n8n-nodes-base.wait",
"typeVersion": 1.1,
"position": [
320,
-320
],
"id": "041baec3-21d0-4c22-aa17-98c4df2a6727",
"name": "Wait",
"webhookId": "d061aa74-58b0-49cd-bb4c-2afa0693b3c4"
},
{
"parameters": {
"jsCode": "// This function will count the number of loops by using the `$runIndex` variable\nreturn {\n loopCount: $runIndex + 1, // Increment the $runIndex to start count from 1\n};"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
540,
-320
],
"id": "ff53df41-1ad4-484a-910a-245b71c85e07",
"name": "Code"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "fb9ea334-facc-4356-8bdc-039ca83b9abd",
"leftValue": "={{ $json.loopCount }}",
"rightValue": 5,
"operator": {
"type": "number",
"operation": "gt"
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [
760,
-320
],
"id": "c9fa917d-94c0-47c0-a384-9fe82255abd8",
"name": "If2"
}
],
"connections": {
"Webhook": {
"main": [
[
{
"node": "If",
"type": "main",
"index": 0
}
]
]
},
"HTTP Request": {
"main": [
[
{
"node": "If1",
"type": "main",
"index": 0
}
]
]
},
"If": {
"main": [
[
{
"node": "HTTP Request",
"type": "main",
"index": 0
}
]
]
},
"If1": {
"main": [
[],
[
{
"node": "Wait",
"type": "main",
"index": 0
}
]
]
},
"Wait": {
"main": [
[
{
"node": "Code",
"type": "main",
"index": 0
}
]
]
},
"Code": {
"main": [
[
{
"node": "If2",
"type": "main",
"index": 0
}
]
]
},
"If2": {
"main": [
[],
[
{
"node": "HTTP Request",
"type": "main",
"index": 0
}
]
]
}
},
"pinData": {}
}
The n8n SWITCH Node
The SWITCH node allows you to branch off in multiple different directions. Inside of the node you set as many routing rules as you like. To make it easier to see in the canvas, rename the output by hitting the slider and adding a new output name.
You set the condition in much the same way as the IF node, matching one expression against a fixed input or another expression.
You should also add a Fallback Outcome in the options at the bottom of the node and decide what you want to see happen if nothing matches. You can select nothing, create an additional output, or match it to one of the other routes you created.
Which Node to Use
Whether you use the IF node or SWITCH node will largely depend on your own use case.
Use the IF node if you only want to split in one or two directions and use the SWITCH node if you need to branch in multiple directions.