NodeJS for non Javascript developer : Part 2

Arun Singh
5 min readSep 13, 2018

--

This article is the continuation of my previous article NodeJS for non Javascript developer and next article of this series is here Part 3.

Photo by Christopher Robin Ebbinghaus on Unsplash

This article will cover some new stuff of javascript which will help you to gain some advanced idea of NodeJS. Let’s start with a few new jargons which you will find more often in NodeJS.

Object : Javascript is an object-oriented programming language and hence we can create a real-world entity like a car, employee etc which is called Object.
An Object can be created using the new operator.

var objOne = new Object();
console.log(objOne);

Now the output will be an empty array and it makes sense because no property has been assigned to the new object.

New property can be added using the dot operator on the object.

Here a property “name” is created for the object objOne and a value “Arun” is assigned to it.

Use of an object inside a function can be done as follow.

Here sayHello is a function and objOne is an object which is passed inside it as an argument.

An object can also be created using below way.

var objOne = {};

Now we understand the object and function. Let’s understand the scope of a variable.

Lexical scope: Variable that is declared outside the function are a global variable and variables that are declared inside the function are local variable.

In above example, globalVar is a global variable and is accessible inside and outside the function, while localVar is a local variable and is only accessible inside the function.

In above example, the value globalVar is updated inside the function and a new value is assigned to it. Unlike other programming languages, once the global variable is updated inside the function, its value will be changed at the global level.

So it can be observed that once the value of globalVar is updated inside the function scope then its value will be updated at the global level.

Null value: Null value in javascript is also know as undefined. Most of the time the word ‘undefined’ can be seen in javascript. In javascript the default value of any variable is undefined.

Here no value is assigned to x and hence when you try to access the value of x, you will get undefined.

Callback function: It is a function that is passed inside a function which will be called at some later point. We will see what it means in the array.

Array: Array in javascript are heterogeneous.

var myArr = [“Hello”, 1];
Here myArr contains both string and integer. This is perfectly valid in javascript. Now to loop through an array, you can use foreach function.

Array.foreach() → this will loop over the entire array.

Now to perform an action on the items of an array you have to use the Callback function.

array.forEach( function(){} );

The function inside the forEach is a callback function. This function will not be called by the user but the forEach will call and process it.

The “num” is an argument which will process each individual data of the array from 0th index and so on. And these individual data can be processed for other purposes like printing etc.

Closure : It is like an object. Lets understand with an example.

In the above example there are few things.
1. globale var : globaleVariable //line number :1
2. function : greet //line number :3
3. local var : name //line number: 5
4. local function : greetName //line number :6
5. called local function: greetName() //line number :10
6. called function greet() // line number :14

In the above example, the line from point 3 to 5 is called closure.

Note: greet() is a parent function and greetName() is a child function.

Once the function or object greet is created, a value to variable “name” is assigned(in this case “world!” is assigned to the variable “name”). This variable is available to all its child.
Confused!!!!! Let’s understand with an example.

In above example, greet is a function that takes an argument “someMessage”. Inside this function, there is a local variable “msg” and a local function greetMessage().

In line 14 and 18 a new object is created (Note: function are also considered as an object in javascript).
And greetMsgOne and greetMsgTwo are now a function that can be used to call the inner function greetMessage(). When you called these two function variable “msg” is shared in both the objects. And that is the closure.

For part 3 click here.

--

--