What is Node.js?

John Ex
4 min readMay 30, 2021

I was contacted to be considered for an apprenticeship and one of the technologies included in the description was Node, and another was Javascript. I honestly thought Node was just what we used to compile Javascript in my IDE but after some research, I discovered things I wish I knew a long time. Node is technically still javascript but it is much more.

What is it then?

Technically speaking it is a runtime environment for Javascript, but what is that? To understand that let’s first look at how Javascript traditionally runs.

Javascript is usually run in the browser, like chrome. Each browser has its own engine in charge of running it. Google has the fastest of those engines and that is what Chrome uses. It is called the V8 engine and that is what runs the code. This is actually something you can access right now on this webpage by pressing F12 or accessing the developer tools and navigating to the console tab, anything written in there will be executed using the V8 engine.

Getting back on topic, Node is basically a C++ program that uses that same V8 engine which allows for more features, and most importantly the ability to create a backend using Javascript!

Why use Node?

There are many things you can do with Node, but the thing that makes this a very attractive technology to learn is the fact that you can use the same language to make the whole stack. Node allows a Javascript developer, traditionally a frontend developer, to now be a full stack developer using Node.

It has a lot of support, is open source, and is very reliable. Many big companies utilize this and it has been proven to save work, time, amount of code, and even fewer files in general, compared to other languages and systems used for the same purposes.

To add to this it is asynchronous, which at first I thought was a bad thing but it is like that on purpose for a reason. In the front end having an asynchronous problem may lead to information or features not behaving properly, when making something like an API it is almost essential to have an asynchronous setup so that everything doesn't come to a full stop while one piece of information is being processed.

Can you still use the same Javascript code?

Some of it. Some core functionality and core methods are different but a lot of it is still the same Javascript code. One of the main things are modules and the fact that variables are not available across javascript files since in this case there will be no HTML file that brings both files together.

Let’s say we have 2 files, one is app.js and the other is variables.js

In variables.js we have the following

let x = 20

and in app.js we have this

console.log(x)

When running app.js we will see that x is not defined in app.js and it doesn’t have access to it.

This can be a good thing for making reliable programs that won’t break if you use the same variable name in multiple names, although it is still generally an important thing to keep names unique and relevant to make your code a lot more understandable.

Another core difference is the absence of window and document objects which in this case are just a feature of running this in the browser, however there are substitue objects. One of these is global which cabn be seen as the replacement since that is where things can be stored. This is also where functions are stored like the below:

console.log, setTimeout, and clearTimeout can still be used the same way but in Node these are stored in that global object we spoke about before so it is automatically telling the program that it is, for example, global.console.log() instead of window.console.log() like normal Javascript would.

Using almost the same test as before, lets look at an example that proves something important. when a variable is defined it is NOT defined in the global score, which is why we don’t have access to it.

Lets use a single file called app.js and give it the following:

let x = 20

console.log(x)

now we will get an output

20

however what if we change that to

let x = 20

console.log(global.x)

we get

undefined

This is because although the variable was defined in the same file, the variable is not defined in the global object, which is how you are able to have an x variable in different files and they wont interfere.

Conclusion

This isn’t meant to teach you how to code in Node, instead its like a lesson 0 to give you an idea of what node is and in what direction you’re heading. By the time you’re reading this I might already have a begginers guide to Node for you to check out. I hope this was a good introduction to Node to clear up some confusion that I had when first introduced to Node. Happy Coding!

--

--