The SIGMA

node js very basic

Reading Time: 3 minutes

මේ post එකෙන් මම කියන්න යන්නේ node js වල ප්‍රදාන දේවල් වන variables, array, array looping, functions,ගැන සරල හැදින්වීමක්.

මුලින්ම බලමු කොහොමද node වලදී variable එකක් declare කරන්නේ කියල.

//variables
//we can use var and const for declare variables 
//const variables can not change after declaration
var name="TheSigma"; // for sring variable use "" and do not forget ;
var number=123; // for number 
const check="this is const variable"

// then we print output using console.log
console.log("name :",name);
console.log("number :",number);
console.log("check :",check);

දැන් අපි මේ code එකේ output එක බලමු. ඔයාල මේ code එක කැමති name එකක් සහ .js extention දාල save කරල ඒ save කරපු file location එකේ command prompt එක open කරගෙන node playground.js කියල type කරලා enter press කරන්න. මේ විදියේ output එකක් ඔයාලට දකින්න පුළුවන් වෙයි.

මන් කලින් කිව්වා const දාපු variables value assign කරාට පස්සේ change කරන්න බෑ කියල. අපි බලමු check කියන variable එක change කරලා.

//variables
//we can use var and const for declare variables 
//const variables can not change after declaration
var name="TheSigma"; // for sring variable use "" and do not forget ;
var number=123; // for number 
const check="this is const variable"

// then we print output using console.log
console.log("name :",name);
console.log("number :",number);
console.log("check :",check);

//now we change check variable
check="value2";
//now print check value
console.log("value after changed : ",check);

දැන් කලින් කරා වගේ run කරල බලමු.

පේනවා ඇති error එකක් print වෙලා තියෙනවා ඒ අපි const variable එකක් change කරපු නිසා :).

Array

දැන් අපි බලමු node වල array එකක් declare කරන විදිය.

declaration

var array = [1,2,3,4];

array එකක variables එකම type එකේ වෙන්න ඕන සහ [] ඇතුලේ තමයි variables ලියන්න ඕන.

දැන් අපි array එකේ values print කරන විදිය බලමු.මේකත් අපිට console.log(array name) දාල print කරන්න පුළුවන්.

console.log(array) //output : [ 1, 2, 3,4 ]

special index එකක value එකක් print කරනවනම් මේ format එකට code කරන්න ඕන .console.log(arrayName[index])

console.log(array[1]) //output: 2

for loop එකකින් array element print කරන විදිය.

for(i=0;i<array.length;i++){ 
	console.log(array[i]);
}

දැන් බලමු map keyword එක බාවිතා කරල array එකක variables print කරන විදිය.

array.map(function(each){
	console.log(each);   //මෙතන each කියල ගත්තෙ for loop එකේ i //කියල ගනිපු variable එක 
})

functions

node වලදි function එකක් declare කරන්නෙ function කියන keyword එකෙන් simplest example එකක් ගත්තොත් හිතන්න ඔයාට variables ගොඩක් print කර ගන්න ඕන මේක සාමාන්‍ය විදියට කරොත් print statements ගොඩක් ලියන්න වෙනව ඒත් function එකක් use කරොත් එක print statement එකෙන් වැඩේ ගොඩ දාගන්න පුළුවන්.

function namePrint(){
	var name1="test 1";
	var number1= 1;
	var name2="test 2";
}

දැන් අපිට මේ function එකේ තියෙන variables print කරගන්න පුළුවන් එක print statement එකෙන්

console.log(namePrint());

අපි numbers දෙකක් එකතු කරන්න වගේ වැඩකට function එකක් ලියනවනම් ඒ function එක ඇතුලට values pass කරන්න වෙනවනෙ ඒක කරන විදිය තමයි පහල code example එකේ තියෙන්නෙ

function sum(a,b){
	var c=a+b;
	console.log("sum of : ",a," + ",b," = ",c);
}

//දැන් function  එකට call  කරමු 
sum(3,5); //output= sum of :  3  +  5  =  8

complete code for this post ->


var name="TheSigma"; // for sring variable use "" and do not forget ;
var number=123; // for number 
const check="this is const variable"

// then we print output using console.log
console.log("name :",name);
console.log("number :",number);
console.log("check :",check);

//now we change check variable
//check="value2";
//now print check value
//console.log("value after changed : ",check);


var array = [1,2,3,4];

console.log(array) 
console.log(array[1]) 

for(i=0;i<array.length;i++){
	console.log(array[i]);
}

array.map(function(each){
	console.log(each);   
})
function namePrint(){
	var name1="test 1";
	var number1= 1;
	var name2="test 2";
}

console.log(namePrint());
function sum(a,b){
	var c=a+b;
	console.log("sum of : ",a," + ",b," = ",c);
}

sum(3,5);