Learning Objective: Let’s play with some features of NodeJS to understand its structure and workings.
Creation of Function and display its output to Console
function sum(no1,no2){
return (no1+no2);
}
console.log(sum(10,20));
Now let’s create our own Library / Module and make it into a separate file (I will create 2 files here, 1 file for the library and 1 file for calling the library)
My Library (mylib.js)
exports.sum = (no1,no2) => no1 + no2;
My File where I will import the above library and will use the library’s function
var mylib = require("./mylib");
console.log(mylib.sum(10,20));
Simple If conditions
no1=0;
if(no1>0)
{
console.log("Positive");
}
else if(no1<0)
{
console.log("Negative");
}
else
{
console.log("Zero");
}
Simple Loop
for(i=1;i<10;i++)
{
console.log(i);
}