Variables, Operators and Functions in Node.js (Part-4)

Akash Jadhav
5 min readJun 4, 2019

--

Introduction -
In this tutorial, we are going to get familiar with the types of variables, operators and functions in node.js

If you need the list of all node.js tutorials available and their end results follow the link below -
Node.js Tutorials Summary (Part-0)
https://medium.com/@akash_jadhav/node-js-tutorials-part-0-a48b44bb3b6e

If you haven’t checked out my previous tutorial about Simple node program (Part-3) follow the link below -
https://medium.com/@akash_jadhav/simple-node-program-part-3-51e9a12d35bb

Note:
1) If you have not setup your node.js environment I would suggest you to view THIS tutorial to get setup with your machine.
2) Follow each step and go through its description to not only make your program running but also to understand it in detail.
3) All commands are to be executed on ‘terminal’. (Ubuntu terminal is used this below tutorial)
4) Link to code files on Github here -
https://github.com/AkashJadhav-github/node-projects

We have dealt with variables in previous tutorials of ours, here we will explore it in more details and learn new entities like different types of operators and functions.

Types of Variables

Consider a variable “number1” from our last tutorial. In node.js we can declare variable in below three different forms -

  1. var number1;
  2. number1;
  3. let number1;

What’s the difference between these three? Why are they using so many declarations?

So to answer your questions I need to introduce you to the concept of ‘scope of variables’
There are two types of scope every variable has,
a) Global scope
b) Local scope

a) Global scope - When a variable is declared globally, it can be accessed by the complete program from where ever it is called. Its open to all i.e. its publicly accessible.
Global variable can be declared using keyword “var” e.g. var number1;
Now the value of “number1” can be accessible to the entire program, called from any function or method. Its value is accessible and can be changed by any one in the program.
You can declare Global variable anywhere inside the program. Either on top, bottom or inside any method.

b) Local scope - If a variable is declared inside a particular function, it’s value is not accessible outside that function and this is called Local scope of the variable. Only the function where the variable is declared, can access its value (if the variable is not declared global).
Local variable is declared using “let” keyword e.g. let number2;
Function in which variable is declared as local can only access its value, rest are not allowed to access it or we can say that they cannot find this variable with local scope.

So now coming back to the point of Types of variables, type 1 and type 2 are Global variables. i.e. global variables can be declared in type 1 and type 2 format.
Type 3
is type of local variable i.e. local variable can be declared in type 2 format.

( IMPORTANT -
I suggest not to use type 2 for declaring global variable because when the program runs, it will not create an instance of the variable and it will use it from the same memory address.
Usually it happens that, when a program is called/run/executed, it creates an instance of all the variables in memory and keeps them separate from another time execution of the program. That means, if another person runs the same program for the second time, for him, again separate variable instances will be created in the memory and those will be used for him.
If we are not using “var” keyword it will use the same variable for every execution of the program it will use the same variable value and every program will update the same value. So the value will hold result of multiple programs which is wrong in ideal case.
E.g. -
number1 = 2 [set by the 1st program]
number1 = 5 [set by the 2nd program]
so, the value will be 5 for number1 variable for both the programs.

In other case, if we declare global variable using “var keyword, then following will be the scenario -
var number1 = 2 [set by the 1st program]
var number1 = 5 [set by the 2nd program]
so, the value will be 2 for number1 variable & 5 for number2 variable as these both variable has different location in memory as they both are different instances. )

Operators

Here I will introduce you to mostly used operators i.e. arithmetic operators, rest operators is your task to find out from link mention below -
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators

Arithmetic operators -
1. ‘+’ — addition
Example: 2 + 3 =5

2. ‘-’ — subtraction
Example: 8 - 3 =5

3. ‘*’ — Multiplication
Example: 8 * 3 = 24

4. ‘/’ — Division
Example: 8 / 3 = 2 (yes, the answer is 2 and not 2.6 because integer is divided by integer which evaluates to integer. What if integer is divided by fraction? I leave it to you to find out as task.)
Example: 9 / 5 = 1

5. ‘%’ — Modulus
Example: 8 % 3 = 2 (remainder)
Example: 9 % 5 = 4 (remainder)

6. ‘++’ — Increment
Example (x++):
let x = 5; (value of x here is 5)
print x++; (value of x here is 5. Value of x will increment by 1 here after the statement is terminated i.e. after ‘;’ and this value will be reflected on next line)
print x; (value of x here is 6)
Example (++x):
let x = 5; (value of x here is 5)
print ++x; (the value of x is incremented here itself by 1. The current value of x here is 6 and not 5)

7. ‘- -’ — Decrement
Example (x - -):
let x = 5; (value of x here is 5)
print x - -; (value of x here is 5. Value of x decrements by 1 here after the statement is terminated i.e. after ‘;’ and this value will be reflected on next line)
print x; (value of x here is 4)
Example (- - x):
let x = 5; (value of x here is 5)
print - -x; (the value of x is decremented here itself by 1. The current value of x here is 4 and not 5)

Functions

Functions are the set of instructions which are bind together to perform a specific task.

Function declaration -
function nameOfFunction() {
// function body
// return statement;
}

Example — Let us write addition of two numbers program using functions in Javascript.

var http = require(‘http’);

http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/html’});

var number1 = 2;
var number2 = 2;
var sum;

function sum(number1, number2){
let number3 = number1 + number2;
return number3;
}

sum = sum(number1, number2);

res.end(‘Sum = ’+sum);
}).listen(8080);

Now, I will walk you through each line with explaination -

function sum(number1, number2){
let number3 = number1 + number2;
return number3;
}

Above is the definition of the function i.e set of instructions function need to perform.
function sum(number1, number2) is the definition of the function. This function accepts the two parameters/values from the calling statement/source.
Next, the function uses these two variables, add them and return its added value to the calling statement/source.

sum = sum(number1, number2);

Above is the statement where the function is called i.e. it is called to get executed. We need to pass two parameters to the calling statement and we get the sum in return. We need to capture/store this returned value in some variable. So we have assigned the function called to a variable value i.e. “sum
Now this “sum” variable will have the result of addition of two values.

Feww, it was exhausting right? so many concepts to keep in mind.. Don’t worry it’s simple. Just take a break, drink fresh water and rejuvenated yourself and then revise!

Finally, If you like it, give much more claps and Happy Nodeing!!

--

--