Post image

Many of you have heard about Node.js being used by various companies these days like linkedin, paypal, medium, uber and many more (you can see a list of various companies here).

In these upcoming series of posts I will help you learn basics of this technology, develop an app, using a no-SQL database with it, deploying an app using AWS. To keep up with these posts, you need prior knowledge of basic coding.
Knowledge of javascript is a plus.

For those who are not familiar with javascript, we are gonna
start with a little introduction of javascript in this post.

Firstly I should tell you there is no connection between java and javascript, the two are distinct languages and differ greatly in their design. Javascript is an interpreted language i.e. it doesn't need to be compiled, we can run the script directly into any web browser(like javascript console in chrome). Earlier javascript was used for browsers only, for the front-end purpose along with HTML and CSS. With introduction of Node.js, javascript entered the field of server side development too.

An example of JS used with HTML:


      <script language="javascript" type="text/javascript">
            document.write("Hello World!")


What does this html snippet does ?
- When we run this HTML, in between it calls a script which simply prints out Hello World! on the HTML page.

Now let's see some basics of JS which you will need while working with node.js:

Defining a variable:

It's as simple as any other language, you just use var keyword
like: var x = 10; or var y = "icanbeacoder"; or var object = {"key":"value"}; or var arr = [1,2,3].
You can also declare variables without using var keyword.

When you use the var keyword the variable is declared within the scope, if you don't use var it works as a global variable until it encounters a variable by the given name.

Printing something on console:

For printing anything on console, use the keyword console.log.
console.log("Hello World");

if-else in JS:

It's the same as C, C++ or any other language.

else if(condition){  
loops in JS:

There are 4 types of loops in JS:

  • for loop : The syntax is same as C, C++ and it works in the same way.
for (initialization; test condition; iteration){  
   Statements to be executed if test condition is true
  • for/in loop : The for/in loop is used to loop through an object's properties.
for (x in object){  
   statements to execute till the... 
   ..object iteration is not finished

In each iteration, one property from object is assigned to x and this loop continues till all the properties of the object are not covered.

var sentence = {a:"i", b:"can", c:"be", d="a", e="coder"}; 

var text = "";  
var x;  
for (x in sentence) {  
    text += sentence[x];

In the above snippet the final value of text is icanbeacoder.

  • while loop : It's same as C,C++.
while (condition) {  
    statements to be executed
  • do/while loop : It's same as C,C++.
do {  
    code block to be executed
while (condition);  

JS has two types of comparison operators (== & ===), in code practice it's good to use === operator as it compares both variable and variable type, for example 1 == "1" will return true whereas 1 === "1" will return false.

functions in JS:

The syntax for defining a function is a little bit different here.

var func = function(){  
code block to be executed  

or The traditional style

function func(){  
code block to be executed  
Timing Events in JS:

Javascript gives us two functions to do events basis on time.

  • setTimeout(): This executes a function after a specified time(in milliseconds). Syntax:
    setTimeout(function, milliseconds)
setTimeout(function func(){  
  }, 200);
  • setInterval(): This is same as setTimeout(), but it executes the function repeatedly. Syntax:
    setInterval(function, milliseconds)
setInterval(function func(){  
  }, 200);

For detailed tutorial on JS you can follow up on these websites:
1. mozilla developer
2. tutorialspoint
3. w3schools

In the next article we will start with the installation of node.js and some basics of it.