/**
* To apply recursion
*/
function factorial(number){
if(number<=0)
return number;
var factorial=1;
for(var i=1;i<=number;i++)
factorial = factorial*number;
return factorial;
}
var number = 5;
console.log("Factorial of "+number+" is :"+factorial(number));
/**
* To demonstrate the functions
*/
//parameterized functions
function getSimpleGreetingMessage(personName){
return "Hello, I am "+personName+", Good morning";
}
//parameterized functions with Default parameters
function getGreetingMessage(personName,message="Good morning"){
return "Hello, I am "+personName+", " + message;
}
//rest parameters functions
function getArticulateGreetingMessage(...params){
var message ="Hello,";
for(var i=0;i<params.length;i++)
message = message+","+params[i];
return message;
}
//anonymous functions
var howAreYou = function(name){return name +" How are you..";}
var howAreyouDoing=function(name){return name +" How are you doing..";}
var personName = "Tejas";
var greetings=getSimpleGreetingMessage(personName);
console.log("default Greetings::"+greetings);
greetings=getGreetingMessage(personName,"Good night");
console.log("special Greetings::"+greetings);
greetings=getGreetingMessage(personName);
console.log("default Greetings::"+greetings);
greetings=getArticulateGreetingMessage("Hey","I am Tom", "good evening","wanna watch movie","say MI-6");
console.log("articulate greetings :"+greetings);
greetings=getGreetingMessage(personName,howAreYou("John"));
console.log("special greetings::"+greetings);
greetings=getGreetingMessage(personName,howAreyouDoing("Miki"));
console.log("special greetings::"+greetings);
//Assign functions to variables
var greetMessage = getGreetingMessage;
console.log("Greet to Johan ::"+greetMessage("Johan"));
//passing function variabls as arguments
function gossip(greet,ask,name1,name2){
return name1 + " says :"+greet(name1)+","+ask(name2)
}
console.log("gossip1 ::"+gossip(greetMessage,howAreYou,"Ina","Mina"));
console.log("gossip2 ::"+gossip(getGreetingMessage,howAreyouDoing,"Mina","Ina"));
/**
* To demonstrate if, if elseif ladder, switch case
*/
var numerical = 10;
if(numerical>0)
console.log("it is positive number")
if(numerical>0)
console.log("it is positive number");
else if(numerical<0)
console.log("it is negative number");
else
console.log("it is ZERO");
var fruit = "Mango";
switch(fruit){
case "Mango":{
console.log("It is green, orange or yellow in color");
break;
}
case "Watermelon":{
console.log("it is green in color");
break;
}
case "banana":{
console.log("it is green or yellow in color");
break;
}
default : {
console.log("i dont know color of this fruit : "+fruit);
break;
}
}
Operators represent essential part of any programming language. ECMAScript standard as well supports rich set of operators. Very Few of them are demonstrated below.
/**
* There are sets of arithmetic, relational, logical, bitwise and conditional operations
* This will demonstrate few operations to be done on variables
*/
var num1=20;
var num2=10;
console.log("Addition :: "+(num1+num2));
console.log("Modulus :: "+(num1%num2));
console.log("pre increment :: "+(++num1));
console.log("Grater than :" + (num1>num2));
console.log("bitwise And" + (num1&num2));
console.log("bitwise OR" + (num1|num2));
Hoisting term refers to way variables are declared in javascript. if we declare variable with VAR as keywords they will be hoisted at beginning of script during compilation. That means declaration will automatically move to start of file in global scope. LET keyword does not support concept of hoisting. Hence if we want to declare variable with local scope we must use LET.
/**
* To demonstrate the var variables support hoisting and let variables do not.
* hence if var is declared inside blocks they are still accessible outside due to hoisting effect.
* As a part of hoisting all declarations are moved to the beginning of function or file.
*/
function sayHi(){
for(var i=0;i<5;i++){
var message1 = "I am Tina";
console.log(i+" : inside for loop message1 ::"+message1);
let message2 = "I am Hina";
console.log(i+" : inside for loop message2 ::"+message2);
}
console.log(i+" : message1 :: "+message1);
//since i, message are declared by var, they support //hoisting, hence they live even outside of block where they were declared.
//console.log("message2 :: "+message2)
//since message2 is declared by let, it was not hoisted hence //it lives upto end of //the for block.
}
sayHi();
Const keyword is used to declare constant variables. Immutable is the one whose value cannot be modified. Const is the one whom we can assign value only once.
/**
* to demonstrate the const variables, const variable are immutable
*/
const PRIMARY_VALUE = 100;
//PRIMARY_VALUE=200; second time assignment not allowed
console.log("PRIMARY_VALUE:::"+PRIMARY_VALUE);
const NUM_ARRAY = [1,2,3,4,5,6,7,8,9];
NUM_ARRAY.push(10);
//NUM_ARRAY = [11,12,13,14,15];//since array is const, reinitialization is not allowed. it will throw error
console.log("NUM_ARRY::"+NUM_ARRAY);
To demonstrate Local scope and global scope of variables.
/**
* to demonstrate local scope and global scope
*/
var message = "I am Tom"
function sayHi(){
var message ="I am Tina";
{
let message = "I am mina";
console.log("Hi,"+message);
//let message=""; //duplicate declaration not allowed by alread declared let variable
}
console.log("Hi,"+message);
{
let message = "I am Hina";//same named let variable allowed to be declared in different block.
console.log("Hi, "+message);
}
}
console.log("Hi,"+message);
sayHi();
ECMAScript or ES is specification of programming language to be used commonly for client side processing on World wide web. It is standardized by ECMA International organization.
Javascript can be termed as concrete implementation of ECMAScript standard. That means Javascript syntax should confirm to standards of ECMAScript & to be supported across all kinds of system.
HelloWorld Example,
HelloWorld.js
/**
*
*/
var message = "Hello World, I am being run by NPM"
console.log("message ::"+message);