# Function node Introduction

## Метаданные

- **Канал:** n8n
- **YouTube:** https://www.youtube.com/watch?v=rfWbuxGghb8
- **Дата:** 24.03.2021
- **Длительность:** 5:38
- **Просмотры:** 9,799

## Описание

The Function Node is one of the most powerful nodes in the n8n system. In this video, we are introduced to this node, what it does, and how it can be used. We will be covering:
- What is the function node?
- The language used by the function node
- Data structure
- Demo: Replicating a set node in a function node

Resources:
- n8n Website - https://n8n.io/
- n8n Documentation - https://docs.n8n.io/
- n8n Community - https://community.n8n.io/
- n8n Workflows - https://n8n.io/workflows/
- n8n Function Node - https://docs.n8n.io/nodes/n8n-nodes-base.function/
- n8n JavaScript Code Snippets - https://docs.n8n.io/reference/javascript-code-snippets.html
- n8n Expressions - https://docs.n8n.io/nodes/expressions.html
- n8n Data Structure - https://docs.n8n.io/reference/data/data-structure.html
- JavaScript Tutorial - https://www.w3schools.com/js/default.asp

## Содержание

### [0:00](https://www.youtube.com/watch?v=rfWbuxGghb8) Intro

in this video we're going to be talking about the function node is one of the most powerful nodes that is available in naden the function node uses javascript to access other nodes manipulate data or perform more complicated tasks that aren't available in other nodes one of the things that you will notice about the function node is that it only has one property and in that property is where you put all of your javascript code one of the most important things to understand about using the function node is how it views data within the workflow information is stored in a workflow in an array called items once a node has finished performing its tasks it then stores the results of those tasks in the items array for that node is then available to be referenced by other nodes by connecting two nodes together you automatically set the start value of the second node's item array to the end value of the first node's item array

### [1:03](https://www.youtube.com/watch?v=rfWbuxGghb8&t=63s) JSON Objects

each item's array have one or more json objects each object has a minimum a property named json which contains a json object itself it is in this last json object that information is stored as json formatted data there is also an optional binary object that can be created as well if you use this binary object you need to include at minimum a data property and value which is base 64 binary encoder data it is also good practice to include the mime type file extension and file name properties along with their values as well to produce the most complete output information

### [1:47](https://www.youtube.com/watch?v=rfWbuxGghb8&t=107s) JSON Array

information in the nan editor ui the items array is represented in the right side of the open node interface when the node is executed this array is displayed using two or three tabs at the top of the open node interface the first two tabs represent the json data in the items array in two different formats the json tab shows the data in json format while the table tab displays the json data as a data table please note that the information that is presented in these two tabs is identical it is just formatted differently occasionally you will see a third tab this binary tab will show up if the optional binary object is included in the items array

### [2:33](https://www.youtube.com/watch?v=rfWbuxGghb8&t=153s) Set Node

now that we have the data piece out of the way let's continue with the function node let's take a simple node the set node and see what it takes to reproduce the functionality of the set node in the function node the purpose of the set node is to assign values to a property there are three different kinds of properties available to the set node number string and boolean to replicate the default output of the function node using the set node we created a number property give the property the name of my variable and then assign the value of 1 to that property we can now execute the set node and we see that we get a json output showing that the property my variable has a value of 1. if we close the set node and then open the function node when we execute the function node we notice that the output from the function node is identical to the output of the set node now let's add another property to each of the nodes this time we are going to create a string property in our set node first we name the property my string and give it a value of cat

### [3:45](https://www.youtube.com/watch?v=rfWbuxGghb8&t=225s) Function Node

and when we execute the node this is the json string that we get from it let's reproduce this using the function node we open up the function node and open up the javascript code field add a line between the first and last lines to create the my string property and assign it the value of cat let's break this down to understand a bit better what it is doing the term items is referring to the items array that we mentioned earlier this is where the information is stored from the workflow the zero in square brackets is referring to the first javascript object in the array why is it a zero instead of a one because arrays in javascript start their index count at zero and go up from there the dot is a separator which breaks up the entire command into different member sections there are two dots in this string indicating that there are three parts to the object reference json refers to the json property of the first object in the items array this is the required property for the naden workflow the my string is a sub property in the json property it does not exist presently so it will be created when the command is executed the equal sign indicates that what follows is a value that will be assigned to the property represented by the previous string the value in double quotes is the value that will be assigned the semicolon indicates that this is the end of the line you can now close the javascript field editor and execute the function node as you can see the output is identical to that of the set node that concludes our video on the function node thank you for watching

---
*Источник: https://ekstraktznaniy.ru/video/15853*