Java Script Functions

How to declare a function?

function  functionName(){

//body of the function

How to invoke a  function?

functionName();

Function Expression

let variable1 = function(){

//body of the function

};

variable1(); //calling the function

Hoisting works with function declarations but does  not work with function expressions.

Eg:

// function Declaration

functionName();

function  functionName(){

console.log(‘Hello World’);

}

Output: prints Hello World

//function Expression

variable1();

let variable1 = function(){

console.log(‘Hello World’)

};

Output: we will not get the output here

Passing values to a function

Eg:

let variable1 = function(value1 ){

console.log(value1)

};

variable(value1);

Function with return statement

Eg:

const calcArea = function(radius ){

let area = 3.14*radius*radius

return area

};

let area = calcArea(radius);

console.log(area);

Arrow Functions- It is a more simpler way of writing functions.

Eg:

const calcArea =(radius)=> {

let area = 3.14*radius*radius

return area

};

let area = calcArea(radius);

console.log(area);

In the case of  arrow functions with one argument only we can even write without parentheses.

Eg:

const calcArea =radius=> {

let area = 3.14*radius*radius

return area

};

let area = calcArea(radius);

console.log(area);

But if there are no arguments or more than one argument parentheses are needed.

If there is only one line of code in the function we can ignore curly braces also

Eg:

const calcArea = radius => area = 3.14*radius*radius

let area = calcArea(radius);

console.log(area);

Difference between functions and methods

Methods are also functions but which are associated with an object or datatype.

Eg:

Let name =’Sam’

name.toUpperCase();

Here toUpperCase() is a method associated with datatype string.

Call back Functions

When we pass a function as an argument to another function then it is called a call back function.

Eg 1:

const myFunc = (callbackFun) => {

let value = 50;

callbackFun(value);

};

//call the function

myFunc(value=>{

console.log(value);

})

Output: 50

forEach( ) Method

It is a method for iterating through an array.

Eg:

let name =[‘name1’,’name2’,’name3’ ] 

name.forEach(element=>{

console.log(element)

})

Output: name1

             name2

      Name3

Eg 2:

let name =[‘name1’,’name2’,’name3’ ] 

name.forEach((element,indux)=>{

console.log(element,indux)

})

Output: name1   0

             name2   1

      name3   2

Eg 3:

let name =[‘name1’,’name2’,’name3’ ] 

name.forEach((element,indux,array)=>{

console.log(element,indux,array)

})

Output: name1   0 [‘name1’,’name2’,’name3’ ]

             name2   1 [‘name1’,’name2’,’name3’ ]

      name3   2 [‘name1’,’name2’,’name3’ ]

We can even write the call back function separately 

Eg:

let name =[‘name1’,’name2’,’name3’ ] 

Const logName = element =>{

console.log(element)

name.forEach(logName());

Leave a comment

Your email address will not be published. Required fields are marked *