Now with NodeJS installed,
let's now see how we use it
by writing our first, very simple
piece of NodeJS driven JavaScript code
that we will then execute with NodeJS.
And for this, I'm in a brand new, empty folder
opened here in visual studio code.
And in this folder, I will create a new file
and I will name it app.js,
but you can name it however you want.
You can also name it site.js,
but I'll just go with app JS here,
since it holds
my main demo JavaScript application
that I want to build here.
But app is a pretty big word here, actually,
because we are going to start
with a very, very simple
demo application right now.
Now here in this app JS file,
let me zoom in a bit,
we are now going to write
some basic JavaScript code.
I'll start by adding a constant,
which I'll name userName,
and in there I'll store a string
Maximilian
So the text Maximilian
and then I'll add a semi-colon.
This is JavaScript code,
which we could have written
for the browser as well.
We are defining a constant here.
Alternatively, as you learned,
we could also create a variable with let.
That's also supported in NodeJS,
and in general, NodeJS is just JavaScript.
So what you'll learn
about JavaScript still applies.
It's the same syntax, the same rules.
There only are some differences,
like for example, in NodeJS,
since it does not execute in a browser,
there is no DOM with which you could interact.
You can select and change HTML elements
because it's now decoupled
from an HTML driven website.
You must never forget that with NodeJS,
we write code that executes
stand-alone on a computer, on a server.
And whilst we might also
be dealing with HTML files,
to some extent,
there is no tight coupling
as we have it in a browser.
So there are differences like this,
but the core is syntax, like Latin, const,
and the kind of values you can work with.
That's the same.
And different now, we have this username.
And then, in another line,
I will console log username.
And that is also something
we did before in the course already,
we also locked something
in the console of the browser.
Now we will not execute this code with a browser,
but we will execute it with NodeJS instead,
but as you will see with NodeJS,
we will also be able to lock this to a console.
It will just not be in the browser dev tools,
but instead in a different place.
Now with all that, if we save that file,
we got this JavaScript file.
How can we now execute that with NodeJS?
Well, as I emphasized multiple times,
we don't execute it in a browser.
Instead, we installed that NodeJS tool,
and therefore we now will use that tool
to invoke and execute this code with it.
Now, NodeJS is not a tool
like Word or Excel or PowerPoint
that you install on your system
and then open by clicking on some icon
or anything like that.
Instead it is a tool that you invoke
through the command line,
and that is something
you might've never worked with before.
So let me quickly introduce you to it.
No matter if you are on macOS or Windows,
you will have a command line utility
installed on your system, because a command line
is the most basic and primitive form
of sending commands and actions
to your operating system.
So the command line is an interface
that allows you, as a computer user,
to talk to your computer
and give it instructions.
You do the same if you launch a program
like PowerPoint,
but there you have a graphical interface.
A command line is basically
the very primitive version of it
where you don't have a graphical interface,
but where you instead write your own commands
that will then be executed.
Now on macOS, you can open spotlight search
and type terminal in there
to open the built in terminal,
which is your macOS command line interface.
On Windows, you can open
the command prompt application
that's installed on Windows,
out of the box, by default
on every Windows version.
And it's this command prompt, or terminal,
which you opened here,
that can then be used for running commands
that will be executed by the operating system.
Now, because we installed NodeJS,
Node will be a command that is recognized
by the operating system.
So we can execute our JavaScript code
with NodeJS,
through this terminal or command prompt.
Now, doing it here,
in this default command prompt, or terminal,
is a bit cumbersome,
because you would, first of all,
have to navigate into the folder
where your code is stored
inside of that terminal.
Because by default,
this terminal will be navigated
into some folder on your system
and you need to change to the folder
that contains your recode files.
Now, it might be interesting to learn a bit more
about this terminal and command prompt
and therefore attached you find a link
that allows you to dive a bit deeper,
but it's not the focus of this course,
because we also have a way easier way
of executing our code
and of running our terminal or command prompt
focused on this project folder.
And for this, we go back to visual studio code.
In there, you will have this terminal option
here in your menu.
And in there, you can click on new terminal,
both on Windows and macOS.
Now, if you click there,
this opens up an integrated terminal,
or a command prompt,
which is your default system command prompt,
or terminal.
So the tool we just opened
a couple of minutes ago,
but integrated into visual studio code,
which is convenient,
because now you can see your code
and issue operating system commands
from the same screen.
And it's also convenient,
because if you open this terminal
here in visual studio code,
it will already be navigated
into your project folder.
So if you want to invoke the Node tool
to execute the app JS file,
we can just type the command
in here in this terminal,
and it will automatically
be in this folder already.
So, interacting with our code file here
will be very easy
if we use this integrated terminal.
And to show you what I mean,
let's now use this terminal,
which we just opened up here,
and in there type
node
blank
app.js.
So the Node command, which is a command
I want to send to my operating system,
and then after a blank, the file name.
Now, the Node command will be available
because we installed NodeJS, this tool.
And this file name
is simply the file name I chose here.
If you now hit enter,
you should see your username here
in this terminal.
So we see the username we chose here,
here in our terminal because of console log.
And that's now the result
of us executing this JavaScript code
with this Node tool through this default system
command prompt, or terminal,
and we're using the integrated terminal here,
so that we see it all in one screen,
and we run this command,
already navigated into this project folder.
Because if I would run the same command
in my default terminal, or a command prompt,
open outside of visual studio code,
this will most likely fail
if I execute this.
You see here, I'm getting an error
that it essentially can't find this app JS file.
It might succeed for you,
if you did store your code file
in the same folder
as the terminal is navigated in, by default,
but most likely, that's not the case.
So here you would have to
switch to your project folder first,
with the CD command.
More on that can be learned
with help of the attached link,
but that's a bit cumbersome,
And hence, we use this built in terminal instead.
Now, you can also always close
this integrated terminal
by clicking here on close panel
or the trash icon.
And then you can always reopen it if you want to.