6. JavaScript Variables - SCAI017
Sloup
1 follower
1 Video View·Sep 15, 2025
Nate Herk - AI Automation Society Plus\3. Depp Dive Building AI Agents\1. Foundations\6. JavaScript Variables - SCAI017
Hey guys, so just wanted to talk about JavaScript variables real quick because it's a super important thing to understand when it comes to
actually getting data to move from the front of your workflow to the end of the workflow or
having agents send information between each other and
Just really important aspect of automating workflows and creating agents
so
we're just gonna build out some simple flows and some simple examples just to look at what these variables look like how they move and
Maybe some best practices as to how you can reference things and set things. So
We've got a manual sugar here
Nothing's going on and what we're gonna do is we're just gonna grab a set node
To just set some sets and data up front and what we're gonna do is call this actually
No, let's leave this as edit fields for now, and I'll show you guys why I'm doing that here in a sec
But anyways, let's say we want one field to be name or Nate
Sorry, we want this to be named so I can put my name in the next one
Which is Nate and then we're gonna add a field called age and I'm gonna throw in my age
So if we test up we on the right, we're gonna get two variables. So
we have Nate in the field of name and then we have
22 in the field of age and so if we click on JSON, we can see how they're coming back
Understanding JSON is pretty much just gonna be key value pairs where the key is a name and the value here is Nate
Same thing with age. So anyways, we'll keep this as schema for now
Actually scheme is the way I usually like to keep it because it just looks very
Dragon droppable, I guess is a good way to describe it. But anyways, so let's say we've got that, right?
We've got name and age coming through and now we want to push this into a message model node. So
We're grabbing message model. Let me choose a model real quick. I don't want to use open router
I got to get rid of all these credentials now that they have a native open router node as well as a native deepseek node
In an 8n with the latest update that dropped today
Anyways, open AI I'm gonna grab my GPT for a mini and we're just gonna do a system message for now. So
first thing because I see a lot of people when they're first starting out with an 8n is
They want to reference variables, but they're not changing this to expression. So
If it's fixed the model or the node is going to be looking at whatever you actually type in here
But if it's an expression you can drag in your variables and then every single run it's going to be different
So if this is a form response and people are putting in their name and their age
Obviously, it's gonna be different every time and the model is going to process it differently rather than just referencing
for example
JSON name as you can see we have that's the variable JSON name and it's referencing name which is Nate
But if we have this is fixed and we run this it's not going to see anything but literally JSON name
So that's the difference there it if it's a variable and it's a JavaScript variable
It's gonna be green kind of gray undefined or red and that's how you know, it's gonna be an expression
but
You know best practices you might as well always just get in here and flick everything to an expression because I don't know why you wouldn't
But anyways, um, so let's say in here we want to say
please write a
Biography
No, not a biography. Yeah. Yeah, let's just write a short bio
for
Nate
Who is and then we're gonna grab age so we can drag an age who is age
Years old which on the right side as you can see the actual result of this expression is please write a short bio for Nate
Who's 22 years old so we can go ahead and test the step?
It's gonna give us a content message. It is a 22 year old aspiring entrepreneur the passion for technology
growing up in a small town the mobile bar
It is already known for his creative
Okay, interesting, I'm wondering if it's getting some of that from
My open AI account, but anyways, um now let's say we wanted to reference these variables back again. So let's just grab a
Let's just do another set node
Anyways, so let's say we wanted to set this again to
Person, but we just want to grab the name of the person from earlier. Now. We have an option over here where we can
Look through these past nodes and be able to drag stuff back in. So if we wanted to just reference for some reason
the bio
What we would do is we would be able to drag in the content right here
And once again, this needs to be an expression. Otherwise when it goes through it will be nothing so as you can see expression
but if we do fix it's gonna come through just as JSON dot message dot content, so
There's our expression coming through
But as far as person what we want to do is we want to grab the name
So oops, so coming from here we have name and obviously that's going to be Nate
But as you notice when we're referencing the bio what we have is JSON dot message dot content and we're getting this because
Remember in here. Let me go to table view
Or let's go to JSON view. So it's referencing the JSON. It's referencing the message
And then within the message and the JSON we have role content or refusal
So enroll we have assistant and content we have the actual content we're looking for and then in refusal we have null so
if we were to come back into this node and
Let's get rid of JSON dot message dot content. We're gonna put in the two curly braces, which means okay
We're going to be entering some sort of variable. We have some options here, right?
JSON is
Returning the JSON input data to the current node for the current item
So if you reference using JSON, all it's gonna do is it's going to grab the most previous node
So that's why it's grabbing stuff from open AI. So as you can see here in
JSON on the right we're getting objects index message role assistant content body all that kind of stuff
So JSON is what we're sort of the node. We're referencing then we put a dot we can see what comes next
So we know that we're gonna be looking for the object inside this JSON, which is just called message
So we click on that now we have sort of drilled down our search a little bit
And if we put another period we can see now do you want content refusal or role?
So if we were to click roll, this is gonna be coming back as assistant if you remember when we looked JSON
But we don't want that
What we want is the actual content of the open am node which is content and now we have just drilled down to what we're looking
For obviously that's the tough way to do it easier way is just to see okay over here
We want the content or we want the assistant and that's how that kind of stuff comes back, right?
Okay, so that's super cool. But if you notice here we have the suggested options
But we also have the options to reference earlier nodes
So let's say we wanted to reference the stuff back from the previous node, which is called edit fields
We can grab that and now we can look inside here
So we're looking inside the item and now we can see we have the JSON with name age or the paired item
And so if we put another dot we can grab a JSON and then from there we can grab the age or the name
So let's just go ahead and grab the age here and we can see it's 22
And that's what we got here was edit fields dot item dot JSON dot name
so
That's how that works, but if we were to come in here and change this name to
like grab
Info. Okay. I'm gonna go with information because I typed an R so grab information now when we come in here
This is no longer being referenced. We can't determine which item to use. It's red because
The node got changed to grab information, which as you can see, it's pretty much still referencing all that
But we just need we need to come in here and do it again. So grab information
dot item
Dot okay
Let's just do it the easy way
So there's name grab information dot item dot JSON that name and the reason why it's not showing up is because we just have to
Pretty much run the workflow again, so we'll do that
Now it's going to have access to that information from over here as you can see it was red now
It's green
So sometimes when you change stuff, it's gonna show up as red
But you're gonna have to just rerun it that happens a lot
actually, if you start to introduce loops into your workflows for some reason, I'm hoping they'll fix this eventually, but
the
The nodes have trouble referencing previous stuff that's been looped if that makes sense at all
So it's sometimes hard to test workflows each step of the way if you have a loop you have to run the entire workflow
but anyways, um
One thing that's important to know is so like we're in here and we want for the bio
To grab back bio content and it's gonna if you drag it in which is the easiest way to do it
It's gonna be JSON dot message dot content. Let's say though
We forgot that in here. Okay. Why is that all buggy? Okay, we forgot that in here
we needed to do something so let's just do a
filter and
We will just filter
This is equal to I mean this is coming through as assistant
So let's just say that this is equal to assistant and we'll filter that through. So there we go
That item is kept but now in here
Okay, actually that's a terrible example because it works but anyways what I was gonna say is
This was referencing the content from the open AI node
And if we were to have put something in here, that wasn't a filter because that was a horrible example
Let me just do something else and this is actually really bugging me. Why is that?
Okay, let's do something else. What's something that would actually get rid of the previous item. I
Guess we'll do a limit
Okay, I don't know what's going on here
I'm gonna refresh this page because I'm not able to click on that plus
So the idea is that if we were later moving things around if we're always referencing the absolute node, oh my gosh
I don't know. Why is this still bugging?
Okay, I guess that works if we're always referencing the absolute node. It's not going to matter
what what order we drag stuff and move stuff around because
Because it's always going to be referenced as open AI and I don't know why I'm having trouble logically right now thinking of example, but
Let's just do something else here
let's do a
Gmail node. We'll get rid of this guy
and so in here what we want to do is send an email to
email.com with the
Message and then the content the subject will just be
Test we'll make this a text
Okay. So here's I think this should this should be a good example. I don't know why I was
having a
Brain problem earlier. Anyway, so now we can see this was previously coming through as the biography
But it's not because it's not referencing the immediate because this is trying to reference the Gmail node
Which as you can see, there's no json.message.content
But before we added this node, this was perfectly referencing the open AI
So my point that I was trying to make that I made pretty poorly
Was that if we always set up our nodes to reference rather than JSON reference the actual node
So open AI that item that json
message
Content. So now we're getting the bio back as you can see
Once this goes through we're getting the bio back everything, right?
and now we can go ahead and add more stuff in between
this node that we're referencing and the actual node where we're pulling it and it won't matter because we're
Referencing the absolute node rather than just saying json dot something for all of our logic and it you know
It's not something that you have to do. It's just for
It could save you a headache down the line if you're adding in a node in between two things if you want to manipulate the
data a little bit, but
That's pretty much
JavaScript variables how they work how they pass through
Obviously once you're in in here, there's other options you can add like
Well, let me make this an expression
This is how you would go in there and give your agent access to the current date and time
So there's now you can obviously format that a little bit differently if you want to format it like that
This is also how you would in your
Tools for an AI agent you could use the from AI function. So I'm not going to dive into that in this video, but
This is how you would do it
Let's say we're adding a tool right here
which would just be able to the Gmail tool again because you can make these expressions and then once again reference a
Expression called from AI I can find it
Dollar sign from AI now we can use this expression to set keys for each of these parameters and then have AI
Determine based on our query who to send the email to
What the message is gonna be and what subjects gonna be which is just super super cool
when I first started using n8n you basically had to send
The data to a different workflow and pass it through an open AI node that would figure out who it's to who it's from
Sorry who it's to subjects body and then pass that into a send Gmail node, which wasn't a tool it was
Just an actual node so we would have where is it send a message?
So we would have to pass an open AI node
Into this node rather than just being able to hook it up as a tool already
We had to create a whole new workflows a tool. So this is really cool. The from a function is really cool
There we go. I rambled a ton. That's gonna be it for this video. Hope it helped. Thanks guys
