Wouldn’t it be awesome if you have to know only one programming language for building a full stack application. Ryan Dahl put this thought into action and created Node.js. Node.js is server side framework that is built upon Chrome’s powerful V8 Javascript engine. Though, originally written in C++, it uses Javascript for using it in applications.

See, problem solved. One language to rule them all. But this also brings you down to the fact that now your whole application is using the same language. That means you have a sound knowledge of that language. That’s the topic of this article.

Here are the 5 bare minimum concepts that you should be in practice to hang around with Node.js. I’ll try to keep the article short.

1. Non-blocking or Asynchronous I/O

Since Node.js is a server side framework, one of it’s main works is to handle browser requests. In traditional I/O systems, a request can only be issued when the response (the HTML page) of the previous request has arrived. That’s why, it called blocking I/O. The server blocks other requests in order to process the current request which causes the browser to wait (the rotating circle).

Node.js doesn’t follow this principle of I/O. If a request is intended to take longer time, Node sends that request in an event loop (I’ll explain in a different article) and goes on to handle the next request in the call stack. As soon as the pending request is done processing, it tells node and and the response is rendered on the browser.

Lets understand this with a dummy example:

Blocking I/O

In this example of a restaurant, the waiter takes an order, wait for the order to complete and then go back to the table to serve the order each time. During the time the order is processing, the waiter just waits or blocks orders from other customers.

Non blocking I/O

In this example, the waiter takes an order and inform the cook and go back to take another order. After the first order is done processing, it takes that order to the respective table and goes on again to take order from other customers. The waiter doesn’t blocks orders from other customers.

2. Prototype

Prototype is a complex concept of Javascript. Since, you are going to use prototypes a lot in Node.js, every javascript developer must know about this concept.

In a language that implements classical inheritance like Java, C++,for the purpose of code reuse, you first make a class (a blueprint for your objects) and then you create objects from that class or extends that class.

But there is no concept of classes in Javascript. You first create an object in Javascript and them augment your own object or create new objects from it. This is called prototypal inheritence that is implemented through the prototype.

Every JavaScript object is linked to a prototype object from which it can inherit properties. Prototypes are analogous to classes in other OO lanuages but differ in the fact that they itself are objects. Every object is linked to Object.prototype which comes predefined with Javascript.

If you look up a property via obj.propName or obj[‘propName’] and the object does not have such a property which can be checked via obj.hasOwnProperty(‘propName’) , the Javascript’s runtime looks up the property in its prototype object. If the prototype object also doesn’t have such a property, its own prototype is checked in turn until a match is found or if it has reached to Object.prototype. If that property doesn’t exist in the prototype chain, then it results in a undefined value.

Lets understand with the following example code:

When you make a new object, you can select an object that should be its prototype. Here, we are adding a create method to the Object function. The create method creates a new object that uses another object as it’s prototype that was passed to it as an argument.

When we make changes to the new object, it’s prototype remains unaffected. But when we make changes to the prototype object, the changes becomes visible in all the objects that are based on that prototype.

Prototype is a complex concept. I’ll be covering on prototype in detail in another post.

3. Modules

If you’ve ever worked with packages in Java, modules in Node.js in no different thing. If not, don’t worry. Modules are simple Javascript files that contain code for a specific purpose. The module pattern is used to make your code easy to navigate and work with. To use the properties of a module, you have to require it in a Javascript file much like the same as you import package in a Java class.

There are two types of modules in Node.js.

Core Modules — These are the ones which comes in pre-compiled with Node.js library. The purpose of core modules is to provide developers often occurring and repeating code sections that if not available would result in a tedious task for developers because they have to write the same code again and again. Some common core modules are HTTP, URL, EVENTS, FILE SYSTEM etc.

User Defined Modules — User defined modules are the ones which a developer make for a specific purpose in its application. These are required when the core modules are not capable of fulfilling the desired functionality.

Modules are extracted via the require function. If its a core module, the argument is simply the name of that module. If it’s a user defined module, then the argument is the path of that module in the file system.

e.g.,

4. Callbacks

In javascript, functions are regarded as first-class objects. That means you can do all the operations with a functions that you can do with a regular objects. You can assign functions to a variable, Pass these as arguments to methods, declare them as a property of an objects and even can return them from functions.

Callbacks are anonymous functions in javascript that can be passed as arguments to other functions and can be executed or can be returned from that function to be executed later. This is the basis of callbacks which are the most widely used functional programming paradigm.

We we pass a callback function as an argument to another function, we only pass the function definition i.e., we never know when that callback function will execute. That totally depends on the mechanism of the calling function. It is “called back” at some later point of time, that’s why the name. This is the sole basis of the non-blocking or asynchronous behavior of Node.js as illustrated by the following example.

This is one of the simplest example of a callback. We passed an anonymous function as an argument which simply logs some output on the console to the setTimeout function. Since, it’s only the function definition, it doesn’t know when to execute. That determined by the calling setTimeout function via the second argument which is after 2 seconds.

First, the second log statement logs the output to the console and then after two seconds, the log statement in the callback function logs the output.

That’s it. Those were my top 4 Javascript concepts which must be known to a Node.js beginner. What is in your list? Please tell me in the comments.