4 minutes read

You should already know that the Node.js platform allows you to execute JavaScript code without using a browser. In this topic, we'll take a look at the difference between running JavaScript code in a browser and running it with Node.js.

Executing code in the browser

It's worth saying that JavaScript was conceived as a language that adds functionality to pages on the web. Therefore, a lot of things were not thought out in advance, and one of these things is the execution of JavaScript outside the browser.

Few could have imagined that in many years, developers would want to create backend sites in JavaScript and even large and complex applications. You probably know that in addition to the Node.js platform, TypeScript soon appeared as a typed superset of JavaScript. For all these reasons, prior to the creation of Node.js, the developers were executing JavaScript code primarily in the browser.

Let's move on to executing the code in the browser. You don't even need to go to sites that have online interpreters, you just need to have a browser and open developer tools.

On many browsers, developer tools are opened with the keyboard shortcut Ctrl + Shift + I.

In the developer tools, open the console tab, and write any JavaScript code you want. If you want to write multi-line code, then use the Shift + Enter combination to move to the next line. To execute your code, you just need to press Enter, obviously.

If the code is included in the HTML of any page in the <script></script> tags, then more complex processing of such code occurs, which will be discussed later.

Code execution with Node.js

You already know that the Node.js platform uses the V8 engine (as do the chromium-based browsers). This is what allows you to convert high-level JavaScript code into machine code, and the architecture of this engine has changed quite a lot over time. Since chromium-based browsers also use V8, they have become very fast thanks to updates in recent years.

So, let's get down to practice! Here is a fairly simple example: suppose our source.js file is in the Documents folder. You have to open a terminal (this can be done in the IDE too), and make sure you are in the folder with the specified file.

All that remains to do is to write the following command:

> node .\source.js

The dot means that the relative path starts from the directory where we are now.

If your code was outputting something to the console, then you will see the results of your code execution.

This is the most basic version of the command for executing code, and there are many optional parameters. For example, by adding --print-bytecode, you can see a lower-level representation of your code, although it is quite difficult to understand.

The command will look like this:

> node --print-bytecode .\source.js

See the Node.js documentation for more options.

Executing code in the terminal

With Node.js, you can write your JavaScript code in the terminal. It's similar to writing code in the browser console, but with a few twists and turns. To begin with, you should write an even simpler command than the previous ones:

> node

If everything is fine (you have node installed), then you will receive the following similar messages with an invitation to write some code:

Welcome to Node.js v16.14.0.
Type ".help" for more information.
>

The peculiarity lies in the fact that here the Shift + Enter key combination does not work. With the top command, you can write one-line code, although if you've created a variable, you can then refer to it. For example:

> const count = 1;
undefined
> console.log(count);
1
undefined
>

By the way, to display the count variable, you could simply write the count command in the terminal.

The value undefined is returned to you because the variable initialization and variable declaration expressions, as well as the console.log() function, do not return anything.

In order to write multi-line code, you need to write the .editor command after the node command.

> .editor
// Entering editor mode (Ctrl+D to finish, Ctrl+C to cancel)
const sum = (a, b) => {
    return a + b;
}
sum(5, 15);

20
>

Node tells you that after you have written the code, you should use the Ctrl + D combination to finish, or use Ctrl + C to close.

Conclusion

In this topic, we've learned how to execute JavaScript code in a browser and without a browser, using the Node.js platform. We've also touched a little on the structure of high-level code processing, which gave us an understanding that the computer is executing lower-level code.

11 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo