In this video we're going to answer a single question which is what exactly is node j.
S No J.S. came about when the original developers took javascript something that at that point we could
really only run in the browser and they allowed it to run as a standalone process on your machine.
So before node javascript just could not be used as a more general purpose programming language.
It was limited to what the browser allowed it to do.
So for example I could use javascript to add a click event to this button or to redirect a user to a
different page.
But there was no way to use the javascript programming language outside of the browser to build things
like web servers that could access the file system and connect to databases.
All of these are things that other programming languages can do without any problem.
All of this ended up changing with the introduction of node j s with no J.S. javascript developers could
now use javascript on the server side so they could use the same javascript programming language to
create web servers command line interfaces application back ends and more all of which will end up covering
in the class.
So at a high level you know what node is.
Note as a way to run javascript code on the server as opposed to being forced to run it on the client
in the browser.
Now the question has to be asked how exactly is this possible and we're going to explore the answer
by starting with this one sentence summary on the Node J.S. home page.
We're gonna read this out.
Break it down and it's going to shed some light on what exactly is happening here.
So no J.S. is a javascript runtime built on Chrome's v 8 JavaScript engine and it all comes back to
this javascript engine.
Now there are all sorts of JavaScript engines out there.
Pretty much every major browser has their own node j s uses the V8 JavaScript engine and that's the
same JavaScript engine that powers the Chrome browser and V8 is a Google open source project.
The job of the JavaScript engine whether it's V8 or any of the other ones out there is to take in JavaScript
code and compile it down to machine code that your machine can actually execute.
Now the the 8 engine is written in the C++ programming language.
That means anyone out there could write a C++ application they could incorporate the V8 JavaScript engine
into their application and you could extend the functionality that JavaScript provides.
And that is exactly what both chrome and no J S do.
Both chrome and no J S are largely written in C++.
Now you'll notice right here it says that no JSA is a javascript runtime and that's true.
No JSA is not a programming language.
In the end of the day all of the code we're gonna write in this class is indeed javascript code.
The runtime is something that provides custom functionality so various tools and libraries specific
to an environment.
So in the case of Chrome it provides V8 with various objects and functions that allow javascript developers
in the Chrome browser to do things like add a button click events or manipulate the DOM.
Now neither of those features makes sense for a node where we don't have buttons and we don't have a
dumb so node doesn't provide those things.
Instead the node runtime provides various tools that node developers need libraries for setting up web
servers integrating with the file systems so you can read and write from disk and then the end of the
day both chrome and no J.S. are just creating their own modified version of JavaScript.
Now in the end of the day it's still that same core javascript language but with custom functions and
objects injected.
Now this is actually a really important concept so I've put together a quick visualization to walk us
through exactly what's happening here.
So let's take a couple of minutes to jump into that.
As I mentioned we have the Chrome browser and the Chrome browser uses the V8 JavaScript engine.
Now when chrome needs to run some javascript for a particular Web page it doesn't run the JavaScript
itself.
It can't.
Instead it uses the V8 engine to get that done so it passes the code to V8 and it gets the results back.
Now the same exact thing is true with Node node does not know how to run javascript code.
It also uses that V8 engine so a node needs to run a script it passes that script down to the 8 and
in the end of the day it gets those results back.
Now I mentioned that V8 is written in C++ and I also mentioned that both chrome and Node are largely
written in C++ and once again that's no incidents.
The reason that both chrome and Node use so much C++ is that they're both providing bindings when they're
instantiating the V8 engine.
This is what allows them to create their own javascript runtime with interesting and novel features.
It's what allows Chrome to interact with the DOM when the DOM isn't part of JavaScript and it allows
node to interact with your file system when the file system isn't part of JavaScript either.
So V8 has no idea how to interact with the file system with JavaScript or what the dumb is.
It is up to node and Chrome to provide implementations for those when running the eight.
What does this process look like.
Well we have a table.
It's not really a table but it's convenient to think of it as a table and we have a set of JavaScript
methods and objects and we have a set of C++ functions.
So on the left I've picked two very popular javascript methods.
Here I have local storage dot get item which would fetch an item out of local storage and I have document
dot query selector which would allow me to query some elements from the Dom and then do something with
them like deleting them or changing their contents.
Now it might surprise you but neither of these are part of the javascript programming language itself.
You can pull up the specification you can search for them but you're not going to find them.
That's because they're actually implemented by the chrome runtime.
When chrome runs a javascript file that uses either of these in the end of the day some C++ code gets
executed behind the scenes which is responsible for taking care of the functionality.
That's my chrome has so much C++ code no chrome needs to tell V8 what to do when these methods are called
so Chrome's not just passing javascript code to V8 it is also passing down these C++ bindings creating
the context for which the javascript code should be executed.
Now the exact same thing is true with node j s over here I've picked out a couple of node j s methods
that you'll be familiar with by the end of the next section.
I have f asked to read file F S stands for file system and read file allows me to read the contents
of a file from disk.
I also have OS dot platform OS of course standing for operating system and the platform method allows
me to fetch the name of the platform I'm running on such as Windows Linux or Mac.
Now neither of these methods once again are part of the javascript programming language they're provided
to v 8 as part of the node j s runtime.
So javascript doesn't know how to read a file from disk but C++ does.
So when someone uses javascript code and no J S to read a file from disk it just defers all of that
to the C++ function that can actually read the file from disk and get the results back to where they
need to be.
So once again we have a series of methods that can be used in our javascript code which are in reality
just running C++ code behind the scenes.
Now let's wrap up this video by going over a little code example that illustrates some of the differences
and similarities between JavaScript in the browser and JavaScript inside of node j s so to start we're
going to open up any Web site.
I'm just going to use the no J.S. home page and we're gonna crack open the console.
So from here I'm gonna go to settings more tools developer tools and we're just going to pull up the
console tab.
This is going to allow us to run individual javascript statements.
So right here as an example I could type 2 plus 3 hit enter and I get the result 5 back.
I could also create a string with my first name inside of it.
Any string Andrew and I could use the string javascript method to upper case to convert that string
to upper case and I get upper case Andrew back.
Now we can do the exact same thing in note.
Almost all of the note code we're gonna write in this class is going to live inside of files in a larger
project and that'll happen over in visual studio code starting in just a couple of videos.
But for the moment we're gonna go ahead and just mess around with node right here inside of the terminal.
So let's go ahead and use clear this is a standard bash command allowing us to clear the output we're
seeing if you're on Windows.
You need to use C L s. That is the version of clear for your operating system and what we're gonna do
from here is just run the node command.
We're not going to put anything after it just n o d e and we hit enter.
Now what we get here is a little place where we can run individual node statements also known as an
R E P L which stands for read e Val print loop like the terminal it's waiting for us to run some commands
but these are actually gonna be node javascript statements and not bash commands.
So what are we going to do right here.
We're gonna write some javascript code and hit enter exactly like we did over here and I'm going to
use the exact same two things two plus three enter.
What do I get five.
Then I'll create the string Andrew and I'll use these string to upper case calling it.
And what do I get.
I get back to the uppercase string.
Andrew So right here we can see that all of the core javascript features we're used to are still available
when using Node J S because those are provided by the V8 engine itself.
Now let's head over to the browser and start to explore a couple of the differences.
If you've worked with JavaScript in the browser before then you're likely familiar with the window object
the window object is a reference to our browser window.
I can type window I can hit Enter that's going to dump the entire object to the console.
And if we expand it we can view all of the different methods and properties we have access to.
And there are quite literally hundreds of them.
Right here we have some methods related to click events I have on click on change and others.
And if I continue to scroll down we have things related to the window size.
I have outer height outer width and others.
Now all of these make sense in the context of JavaScript in the browser because we actually have a window
to work with.
What happens if I try to get the window in note I'm gonna type window.
I'm gonna hit enter and I can see I'm getting an error reference error window is not defined so window
is something specifically provided by the chrome runtime when javascript is running in the chrome application
node doesn't have a window it doesn't need window so window is not provided.
Now with node we have something kind of similar.
We have a variable called global global stores a lot of the global things we're gonna be accessing throughout
the course.
And if we scroll up we can see we have dozens of different properties and methods available to us.
We're not going to dive into any of these right now but we will be exploring several of them as we start
to learn about node j as now does the browser have global.
The browser does not.
If I scroll all the way to the bottom I can run something new type global hit enter.
What do I get.
I get global is not defined.
I actually misspelled it but I'll get the same result if I do indeed spell it correctly.
Global is not defined.
Now there's another difference.
I'm going to point out before we wrap things up here in the browser I have access to document document
allows me to work with the down.
So right here I have a whole bunch of different elements that make up my page and I can use document
to manipulate them in whatever way I see fit.
Now this makes sense once again for the browser where we actually have a dom.
It doesn't make sense for node where we don't have a dom.
If I tried to print document to the terminal what do I get.
Once again reference error document is not defined.
Now in no JSA we have something kind of similar to document called process.
Process gives us various properties and methods for manipulating the node process that's running.
If I dump this to the terminal we can see there are all sorts of interesting methods and properties
including one exit exit allows us to exit the current node process.
Let's go ahead and actually use it to shut down our little note example right here.
That's gonna be a process which is an object don't exit which is a method I'm gonna call it providing
no arguments since it doesn't need any and all we're gonna do is run this statement by hitting enter.
What do I get.
I can see I'm brought back to the standard terminal where I could run other commands no longer am I
running little node statements now process once again does not exist over in the browser that is specific
to node j s if I type out process and dump it I get process is not defined.
So here we're seeing some of the differences between JavaScript in the browser here specifically chrome
but other browsers would be the same.
And no J S which is javascript running on the server that's where we're going to stop for this one.
Let's take a quick moment to summarize.
No J.S. is javascript on these server.
This is possible because no J.S. uses the V8 JavaScript engine to run all of the javascript code you
provide.
Now no JSA is able to teach javascript new things by providing C++ bindings to the 8.
This allows JavaScript in essence to do anything that C++ can do and C++ can indeed access the file
system.
So now JavaScript can access the file system through node that's where we're going to stop for this
one.
And don't worry about memorizing all of this as we move through the course and start to see this in
action.
I'll bring up the individual pieces again in the next video we're gonna dive into what is also a very
important question.
Why should I learn node j s I'm excited to get to that.
So let's go ahead and jump right in to the next video.