- Now, it's time to create our first Node.js file.
I'm working within the exercise folders
under chapter three, but you can really
create this file within any folder on your computer.
I'm just going to come over here to the explore
and click the plus file icon next to the start folder
and I'm going to call this file firstfile.js
and within our first file, you
can probably guess what's going to happen next
because this is a computer science class
and this is our first file.
We're just going to go ahead
and log Hello World to the console.
So we'll go ahead and save this
and as you can see, we've typed one line
of JavaScript code into this JavaScript file.
It's time to run it.
So, I'm going to go ahead and open up the terminal.
Now, I want to make sure that I'm
in the same folder as my file.
So, I type a ls and we can see
that the first file is within this start folder.
So, to run this file, all we have to do
is type node firstFile.js, and we
can see Hello World is printed on the console.
So, as you can see this is just a JavaScript file
which means we can add JavaScript code to this file.
I can create a variable called hello
and we'll do a message, Hello World From NODE.JS
and I can go ahead and log the variable
in the console instead.
So, now, I can come down here to the terminal
and I can also run this file again
by typing node firstFile, but this time I'm
going to leave off the .js extension.
Because node assumes that it's a Javascript file
that we're running, we don't need
to add that extension there.
So, we can see our message log to the console
Hello World from NODE JS.
So, within Node.js, we can create JavaScript files
and run them, but what exactly is going on here?
On line three, we are logging hello to the console.
The console object is actually a part of a global object.
I could prefix this with global.
and when we run global.console.log
we're going to get the same output.
So, if I come down here and save this
and node firstFile again, we
can see Heloo World From NODE JS.
So, everything on the global object
is available to us globally.
This means we can use any of the objects
or values that are available to us
on the global object within our JavaScript files.
So, the global object contains all
of the objects, values, and methods
that we can use in a Node.js file
without having to import any functionality.
If I come over here to the Node.js Documentation
we can take a look at the global object
and these are all of the values
and objects, and functions that are available
to us globally to use within any JavaScript file.
In this last lesson, we took a look at the Console object.