#2 Running Javascript Outside the Browser

Here is the link to 1st article of this series: What’s node.js and Why we use it.

Ruckaiya Mira
4 min readJul 1, 2022

I hope that you have node installed on your computer at this point and if you don’t, then you can directly install it from its website: https://nodejs.org/en/

Now we’ll go ahead and use node for the very first time. And to get started, create a folder called NODE_SERIES in your desktop. Next, open this empty folder in visual studio code.

We are going to interact with node using the command line and for that I’m going to use the built-in terminal that vs code has. I’m always going to use the terminal that is built-in right into vs code so that we don’t have to jump around between different windows so much.

To start writing some node code here in the console all you have to do is to write node given that you have node.js installed on your computer and then just hit enter and this will then open up the node REPL which stands for Read-Eval-Print Loop.

Here we can write javascript code just like in a normal terminal, for example, we can define variables, expressions etc, that’s because at the end of the day node.js is really just a javascript runtime as we just saw before.

Node commands in REPL

Node.js supports es6 in all the newer versions out of the box without any problem that’s because we’re not running this javascript in any browser but it will always run on the server. In fact, we just took javascript out of the browser and we’re running it inside of this node application.

If we want to exit this REPL i.e. Read-Eval-Print Loop that node gives us, there are different ways of doing it.

First one is to write dot exit (.exit). This exits the node process, the REPL and to start it again- just type node, hit enter and that’s it.

And if you want to clear your terminal hit command k (in mac), type cls (in windows) and that will then clear up the command line.

We used dot exit to exit the REPL but we can also hit control d (and that’s NOT command (in mac) it is really control), so control d will do the same.

Let’s quickly enter node again because there’s some more stuff that I want to show you.

If you hit tab twice you can see all the global variables that are available in node.

Global variables that are available in node

You have all the kind of stuff that we’re already used to, like the array constructor, the string constructor, math, number, etc but then there’s also all kinds of stuff that belongs to node, for example, http, fs or crypto and these are node modules that we’re going to talk more about a bit later but for now you see that we have all kinds of global variables that we can access whenever we want in node.js.

Another nice trick is the underscore variable.

For example, if you calculate 3 times 8 in the terminal, 3 * 8 gives 24 and now if you use underscore times 2 ( _ * 2, i.e. 24 * 2) in the next line, we get 48.

That means that underscore is basically your previous result.

We had 24 and so, underscore in the 2nd calculation is 24 and 24 times 2 gives 28.

In the same manner, underscore plus 2 (here underscore’s value is 48 + 2) gives 50 and so on.

Use underscore to get previous result

Finally, the tab that you just pressed a few moments ago, you can also press that for example, on one of the constructors that we already know like string.

String dot and then by hitting tab twice you can see all the methods or properties that are available to us. You have, for example, the length or hasOwnProperty and all these kinds of methods that we know already.

How to see all methods and properties of any constructor inside node

Now to exit the REPL, all we have to do is

For mac: hit control d or type .exit then command k to clear the console.

For windows: hit Ctrl + d or type .exit then type cls to clear the console.

And that’s all I wanted to show you in this very very first coding with node.

Follow me & subscribe to my email list on medium: https://medium.com/@ruckaiya.awf5 to be notified of the publication of the next article.

--

--

Ruckaiya Mira

Daughter | Sister | Software Engineer | Managing Director @worldschoolofbangladesh | Learn programming like never before.