miliucci.org

Independent software consultant

(currently in love with Bemind.me)

Differences between Node.js and JavaScript

What are the differences between Node.js and JavaScript?” could be a silly question, and one could argue, rightly, that it’s an apples and oranges comparison.

Somebody even asked it on StackOverflow and on Quora, and many times I heard developers using the terms JavaScript and Node.js interchangeably, referring to the code that you can run in a browser or in Node.js.

If these terms are equals, all valid JavaScript statements must run both in the browser and in Node.js, including the following:1

const http = require('http');

Actually it raises an error when run in the browser:

ReferenceError: require is not defined

Let’s try again with another valid Javascript statement, maybe we could be luckier:

document.querySelectorAll("body > div");

This time an error is thrown when it’s run in Node.js:

ReferenceError: document is not defined

My last resort is a simple statement with a help request:

console.log('Help me!');

Fortunately this works on both the browser and Node.js, but this doesn’t prove anything. Instead, the two errors above say clearly that JavaScript and Node.js aren’t the same thing and we need to learn more about them to understand why and how they differ.

The nature of Node.js

The Node.js website says:

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.

Making a little transformation we can translate it to:

Node.js® is a host environment for the Chrome’s V8 Javascript engine.

So we can view Node.js as a container for the Chrome V8 Javascript engine.

But Chrome itself is a container for its Javascript engine… Where are the differences?

JavaScript and the Chrome’s V8 engine

The Chrome’s V8 documentation explains a key concept:

JavaScript is most commonly used for client-side scripting in a browser, being used to manipulate Document Object Model (DOM) objects for example. The DOM is not, however, typically provided by the JavaScript engine but instead by a browser. V8 does however provide all the data types, operators, objects and functions specified in the ECMA standard.

Then it continues with a clear explanation of the V8 job, giving an answer to the question above:

V8 compiles and executes JavaScript source code, handles memory allocation for objects, and garbage collects objects it no longer needs. V8 enables any C++ application to expose its own objects and functions to JavaScript code. It’s up to you to decide on the objects and functions you would like to expose to JavaScript.

Now everything is clear

  • V8 is the compiler and the environment where the JavaScript code is executed
  • Chrome uses V8 to expose the DOM and other APIs to JavaScript in the browser

So, Node.js is a C++ application that wraps the Chrome’s V8 JavaScript engine, exposing a rich set of API to it.2

By the way, if you’re good with C++, you can try yourself building the smallest possible V8 wrapper and maybe build the next Node.js.3

Therefore, the main difference between the browser and Node.js is the environment, the APIs that are exposed to JavaScript. Returning back to our initial statement, we can now say that it doesn’t work in a browser because it doesn’t have the require API:

const http = require('http');

And the second statement doesn’t work in Node.js because it doesn’t have the document (DOM) API:

document.querySelectorAll("body > div");

As we saw, the environment where JavaScript code is executed matters because it defines what statements are valid or invalid.

Javascript host environments

Javascript host environments are many, some examples are:

In every environment you have some available APIs. For example in OSX Automation you have the following global properties:

Automation, Application, Library, Path,
Progress, ObjectSpecifier, delay, 
console.log, ObjC, Ref, $ 

Using them you can retrieve the latest incoming email subject calling:

subject = Application('Mail').inbox.messages[0].subject()

Or filter an array using the whose method:

my_array.whose({ name: 'JavaScript for Automation' })

This filtering method isn’t part of the JavaScript specification, but it’s added to the Array.prototype from the OSX Automation host environment.

Every environment is possibly different from the others, depending on its purpose or specialization, supporting only a subset of JavaScript, and customizing it in many ways. What doesn’t change is the standard JavaScript, the so-called ECMAScript Language.4

The only standard JavaScript: ECMAScript

From ECMAScript specs overview:

ECMAScript is based on several originating technologies, the most well-known being JavaScript (Netscape) and JScript (Microsoft). The language was invented by Brendan Eich at Netscape and first appeared in that company’s Navigator 2.0 browser. It has appeared in all subsequent browsers from Netscape and in all browsers from Microsoft starting with Internet Explorer 3.0.

ECMAScript was originally designed to be used as a scripting language, but has become widely used as a general purpose programming language. A scripting language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. In such systems, useful functionality is already available through a user interface, and the scripting language is a mechanism for exposing that functionality to program control. In this way, the existing system is said to provide a host environment of objects and facilities, which completes the capabilities of the scripting language. A scripting language is intended for use by both professional and non-professional programmers.

ECMAScript usage has moved beyond simple scripting and it is now used for the full spectrum of programming tasks in many different environments and scales. As the usage of ECMAScript has expanded, so has the features and facilities it provides. ECMAScript is now a fully featured general propose programming language.

Conclusion

JavaScript is a good, standardized, language and, as we seen above, it’s available in many environments.5 The browsers and Node.js are only the most common JavaScript environment today, but they must not be confused with JavaScript itself.


  • 1 Valid JavaScript is intended as syntactically correct JavaScript. These examples are, depending on where are executed, SCSW statements: Syntactically Correct Semantically Wrong statements.
  • 2 Actually Node.js is more than a simple “box”: it bundles V8, libio and libuv together creating an event driven architecture. See this simplified Node.js architecture image.
  • 3 Please don’t do it! A fork already happened in the past due to divergences about the right governance of the project. After the creation of the Node.js Foundation, the projects happily merged.
  • 4 JavaScript is the “legacy” or “commercial” name for ECMAScript, that’s it the standard language. You can read more about it in this history of javascript.
  • 5 Please, be sure to use only its Good Parts.