5 Simple Steps To UnderStand Nodejs

To understand how Node.js works, first you need to understand a few key features of JavaScript that make it well suited for server-side development. JavaScript is a simple language, but it is also extremely flexible. This flexibility is the reason why it has stood the test of time. First-class functions and closures make it an ideal language for web applications.

JavaScript has a bad reputation for being unreliable. However, this notion couldn’t be further from the truth. Actually, JavaScript’s bad reputation comes from the DOM’s unreliability. The DOM (docment object model) is the API (application programming interface) that browser vendors provide to interact with the browser using JavaScript. The DOM has idiosyncrasies between browser vendors. However, JavaScript the language is well-defined and can be used reliably across browsers and Node.js. In this tutorial, we discuss a few fundamentals of JavaScript followed by how Node.js used JavaScript to provide a highly performant platform for web applications. Other people complain about how JavaScript handles programming errors (it tries to make invalid code work). However, in such cases, the developers are really to blame, as they need to be careful when working with a highly dynamic language.

Step 1 – Variables

Variables are defined in JavaScript using the var keyword. For example, the following code segment creates a variable foo and logs it to the console. As you saw in the previous article, you would run this code from your console (terminal on Mac OS X and cmd on Windows) using node variable.js.


var foo = 123;
console.log(foo); // 123

The JavaScript runtime (the browser or Node.js) has the opportunity to define a few global variablesthat we can use in our code. One of them is the console object, which we have been using up to this point. The console object contains a member function (log), which takes any number of arguments and prints them to the console. We will discuss more global objects as they are used. As you will see, JavaScript contains most things you expect a good programming language to have.

Step 2 – Numbers

All numbers in JavaScript have the same floating point number type. Arithmetic operations (+,-,*,/,%) work on numbers as you would expect, as shown in


var foo = 3;
var bar = 5;
console.log(foo + 1);   // 4
console.log(foo / bar); // 0.6
console.log(foo * bar); // 15
console.log(foo - bar); // -2;
console.log(foo % 2);   // remainder: 1

Step 3 – Boolean

Two literals are defined for boolean values: true and false. You can assign these to variables and apply boolean operations to them as you would expect.


var foo = true;
console.log(foo); // true

// Boolean operations (&&, ||, !) work as expected:
console.log(true && true);   // true
console.log(true && false);  // false
console.log(true || false);  // true
console.log(false || false); // false
console.log(!true);          // false
console.log(!false);         // true

Step 4 – Arrays

You can create arrays quite easily in JavaScript using []. Arrays have many useful functions, a few of which are shown


var foo = [];

foo.push(1);         // add at the end
console.log(foo);    // prints [1]

foo.unshift(2);      // add to the top
console.log(foo);    // prints [2,1]

// Arrays are zero index based:
console.log(foo[0]); // prints 2

Object Literals

By explaining these few fundamental types, we have introduced you to object literals. The most common way of creating an object in JavaScript is using the object notation, {}. Objects can be extended arbitrarily at runtime. An example is shown in


var foo = {};
console.log(foo); // {}
foo.bar = 123;    // extend foo
console.log(foo); // { bar: 123 }

Instead of extending it at runtime, you can define which properties go on an object upfront by using the object literal notation shown


var foo = {
    bar: 123
console.log(foo); // { bar: 123 }

You can additionally nest object literals inside object literals, as shown in


var foo = {
    bar: 123,
    bas: {
        bas1: 'some string',
        bas2: 345

And, of course, you can have arrays inside object literals as well, as shown in


var foo = {
    bar: 123,
    bas: [1, 2, 3]

And, you can also have these arrays themselves contain object literals,


var foo = {
    bar: 123,
    bas: [{
        qux: 1
        qux: 2
        qux: 3
console.log(foo.bar);        // 123
console.log(foo.bas[0].qux); // 1
console.log(foo.bas[2].qux); // 2

Object literals are extremely handy as function arguments and return values.

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest