Welcome back. Let's expand on our Node knowledge from the previous video and actually create a script file
that we can run.
Let's just do the 'touch' command here again to create a file and we'll just call it 'script.js'.
You'll see over here on the left-hand side that I have 'script.js' in my Node folder.
Now within this folder I can now do anything in JavaScript.
I can say 'const a = 5'.
And that's not the folder that I want ... we want to open this and we'll say 'const a = 4'
'const b = 5'
And then we can just 'console.log(a + b)'. If I save this in order to run this script,
all I do is 'node' and then the script name: 'script.js'.
And because we're in the directory it'll know what 'script.js' is.
And it'll give us 9.
How cool is that?
Now you also see that it exited; it didn't stay in the program, and that is because at the end of the
file, once it reads everything and executes it
– so the last thing it executes is 'console.log' – it runs 'process.exit' and exits out of the file. Let me show
you something.
If I did something called 'setTimeout', which we actually have in Node; if I do
'node' 'global.setTimeout', I get a function.
So if I do 'setTimeout', I can run a function again using our arrow functions.
And within here I can do 'console.log()', and 'setTimeout' is cool because it allows you to say, "hey, when
do you want this to be executed?"
And that's the second parameter and I'll say 3 seconds.
So if I save and I do 'node script.js' – one, two, three, and there you go: 9, and then 'process' exits
Node is smart enough to run through this
once this is done, ends the process and comes back to the Terminal.
Very cool.
And again within node we can actually use
things that might be useful for us based on what global objects we have.
For example one of the most popular ones are '__dirname' - if I save this and run
'node script.js' ...
I get my directory: 'node'.
And I'll show you why that's useful when we start building a server.
But as you can see, we have the power of the browser and running the scripts that we want right here
in our Terminal.
I'll see you in the next video, bye-bye.