ranjithsrt1

Ranjith srt

Posted on August 31, 2024

js | Functions |
Functions
Enter fullscreen mode Exit fullscreen mode

Functions are pieces of code that we can reuse again and again in our code

Function Declaration - JavaScript Hoisting

syntax:
Enter fullscreen mode Exit fullscreen mode
function functionName( ){

// code block

}

functionName()  //calling function
Enter fullscreen mode Exit fullscreen mode
Example:
Enter fullscreen mode Exit fullscreen mode

function msg( ){

console.log("Hii...Hello");

}

msg()  //calling function
//msg()

Enter fullscreen mode Exit fullscreen mode
output:

Hii...Hello
//Hii...Hello
Enter fullscreen mode Exit fullscreen mode

A function declaration defines a named function.It's hoisted,
meaning you can call it before it's defined.

Calling the function:
Enter fullscreen mode Exit fullscreen mode
HOSTING
Enter fullscreen mode Exit fullscreen mode
msg()  // valid  HOSTING

function msg( ){

console.log("Hii...Hello");


}
Enter fullscreen mode Exit fullscreen mode
output:

Hii...Hello
Enter fullscreen mode Exit fullscreen mode

Function Parameters and Arguments

syntax:

                        //input 
function functionName( parameter ){

// code block   //output

}

functionName( arguments)  //calling function

Enter fullscreen mode Exit fullscreen mode

Functions can take parameters, which act as placeholders for the values
that will be passed to the function.The passing value is called an argument.

function msg( name ){

console.log("Hii...Hello" + name);

}

msg( "ranjith") ;
msg( "kumar") ;


output:

Hii...Hello ranjith
Enter fullscreen mode Exit fullscreen mode
multiple parameters
Enter fullscreen mode Exit fullscreen mode
function msg( name ,age){

console.log("Hii", + name +" my age"+age);

}

msg( "ranjith",25) ;
msg( "kumar",20) ;

Enter fullscreen mode Exit fullscreen mode

Default Parameter

function printer(color){

console.log("print document in ${color} color");

}

//printer("blue") 
printer("blue") 

Enter fullscreen mode Exit fullscreen mode
output:

document coloe blue color
Enter fullscreen mode Exit fullscreen mode

//red override
function printer(color="black"){

console.log("print document in ${color} color");

}

//printer("red") 
printer( ) 

Enter fullscreen mode Exit fullscreen mode
output:

//document coloe red color

document coloe black color
Enter fullscreen mode Exit fullscreen mode
 Function with Return Type
Enter fullscreen mode Exit fullscreen mode

function add( a,b){
     return a+b;

}

 let sum = add(10,20); //variable vechu assign panni print pannrom
 console.log(sum);  //30

 //console.log(add(10,20);   check pls

Enter fullscreen mode Exit fullscreen mode
 Function Expression
Enter fullscreen mode Exit fullscreen mode
A function expression defines a function inside an expression.
It's not hoisted, so you can't call it before it's defined.
Enter fullscreen mode Exit fullscreen mode
syntax:

    variable      keyword
let functionName=function ( ){

   //code block

}

functionName()   // calling function

Enter fullscreen mode Exit fullscreen mode
Ex:

           //don't change 
 let msg = function( ){   // function() indha typelaa kandippa irukkanum

 console.log("good morning");

}

msg( ) ;  //good morning 

Enter fullscreen mode Exit fullscreen mode
With Argument
Enter fullscreen mode Exit fullscreen mode

//msg( "ranjith") ;   // exp not hositing

let msg = function( name ){

console.log("good morning ${name}");

}

msg( "ranjith") ;  //good morning ranjith

Enter fullscreen mode Exit fullscreen mode
Function Expression with Return Type
Enter fullscreen mode Exit fullscreen mode

let mul = function (a,b){
   return a*b;

};

let result = mul( 5,2 ){
console.log(result); //10

 //console.log(mul(4,7));

Enter fullscreen mode Exit fullscreen mode
 Arrow Function
Enter fullscreen mode Exit fullscreen mode
 Arrow functions provide a concise syntax and do not bind their own 'this'. They are not hoisted.
Enter fullscreen mode Exit fullscreen mode
 syntax:  
Enter fullscreen mode Exit fullscreen mode

    variable      keyword
let functionName=( ) =>{

   //code block

}

functionName()   // calling function

Enter fullscreen mode Exit fullscreen mode
Example:
Enter fullscreen mode Exit fullscreen mode

let evening = ( ) =>{

 console.log("good evening everyone");

}

evening ()   // good evening everyone 

Enter fullscreen mode Exit fullscreen mode
With Argument
Enter fullscreen mode Exit fullscreen mode
//not hoisting
Enter fullscreen mode Exit fullscreen mode

let evening = ( name ) =>{

 console.log("good evening " + name);

}

evening ("ajith")   // good evening ajith

Enter fullscreen mode Exit fullscreen mode
 Arrow Function with Return Type
Enter fullscreen mode Exit fullscreen mode


 let sub =( a,b ) =>{
    return a - b ;
 };

  console.log( sub(10,6); // 4

Enter fullscreen mode Exit fullscreen mode
Shorter Way
Enter fullscreen mode Exit fullscreen mode

let sub =(a,b) => a-b;

console.log(sub(10,6)); //4

Enter fullscreen mode Exit fullscreen mode
Function Calling Other Function
Enter fullscreen mode Exit fullscreen mode
                       //ranjith
 function welcomeShopper(name){

 console.log(" welcome ${name} ! enjoy yoyr shopping experience")

}              //ranjith
    function main(name){

   // welcomeShopper(name); // direct calling
                   //ranjith
   let ShopperName-name;  // variableassign and after calling
                   //ranjith
   welcomeShopper(ShopperName); // calling top welcomeshopper

  };

  main("ranjith")  // welcome ranjith ! enjoy your.....

Enter fullscreen mode Exit fullscreen mode
Anonymous Functions: Later on Course on Arrays
Enter fullscreen mode Exit fullscreen mode

serTimeout(() => {

   console.log(" anonymous function executed");

 },2000 // 2sec delay  

 output: anonymous function executed

Enter fullscreen mode Exit fullscreen mode
Scope of variables will on functions and loops
Enter fullscreen mode Exit fullscreen mode
var: Function scoped.
Enter fullscreen mode Exit fullscreen mode
Ex:
Enter fullscreen mode Exit fullscreen mode

function demo(){
    var a =20;
    console.log(a); 

  }
demo();
console.log(a)  //error function outside calling

Enter fullscreen mode Exit fullscreen mode
let: Block scoped.
const: Block scoped.
Enter fullscreen mode Exit fullscreen mode
 Ex:
Enter fullscreen mode Exit fullscreen mode

 function demo(){
     var a =20;
     console.log(a); 

   if (true){

   var x = "var";
   let y = "let";
   const z = ""const;     /// block scop

   console.log(x);
   console.log(y);    // all working good ...if block
   console.log(z);

  }
   console.log(x); // outer block code run successfully  (var)   
   console.log(y);   // error  (let)
   console.log(z);   // error not defienfd  (const)

 demo();

console.log(a)

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
ranjithsrt1
Ranjith srt

Posted on August 31, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related