Javascript function

Javascript function

What is a function?

A function, in javascript, represents a block of code that can be executed at any time when the function is called.

The different ways of declaring a function:-

  1. Function declaration.

  2. Function Expression.

  3. Arrow Function.

  4. Nested Function

Function declaration:-

A function declaration is a conventional method to declare a function.

function myName(){
   // Set of statements
}

Example:-

function myName(firstname,lastname){
       return firstname + lastname 
}
console.log(myName("Anksuh", " kumar"))

Function Expression:-

A function expression is similar to the function declaration without the function name. function expression can be stored variable assignment and then use that variable as a function. it is called function expression.

var user = function(para1, para2) {
    // Set of statements
}

Example:-

var user = function(val1,val2){
    return val1 + val2
}
user(10,15)

/* 25 */

Arrow function:-

Nested function:-

A function can have one or more inner functions. These nested functions are in the scope of the outer function. Inner function can access variables and parameters of the outer function. Outer function cannot access variables defined inside the inner function.

Example:-

function A(x){
    function B(y){
        function C(z){
        console.log(x + y + z)
        }
        C(3)
    }
    B(5)
}
A(4)

/* 12 */

Parameters and arguments?

When we define a function that time we will call them as a parameter and after the function declaration when we call that function and put the value on it that is called an argument.

function funName(name){ //parameter
    console.log(`Hey this is a ${name}`)
}
funName("Ankush") //argument
//Hey this is a Ankush

What are rest and spread operator?

Rest:-

The rest parameter does not restrict the number of arguments that can be passed to the function.

Example:-


function sumOfNumbers(...items){
    let total = 0
    for(let i of items){
        total += i
    }
    return total
}
console.log(sumOfNumbers(1))
console.log(sumOfNumbers(1, 2, 3))
console.log(sumOfNumbers(1, 2, 3, 4, 5, 6, 7, 8, 9))

Output:-

1
6
45

Spread:-

The spread syntax is used to expand an iterable object into the list. Spread operators can be used by passing each element of the list object as arguments.

Example:-

1.
let arrItems = [1, 2, 3]
function sumOfNumbers(x,y,z){
    return x + y + z
}
console.log(sumOfNumbers(...arrItems))

2.
let arrItems = [1, 2, 3, 4, 5, 6, 10, 80, 100]
function sum(...items){
    let total = 0
    for(let i of items){
        total += i 
    } 
    return total
}
console.log(sum(...arrItems))

output:-

1.  6
2.  211

Callback function?

The Function that we pass as an argument to another function, is called a callback function.

function firstFun(name,callback){
    console.log(`Hey this is a ${name}`)
}

function secondFun(age){
    console.log(`and your age :${age}`)
}

const data = secondFun(22)
firstFun("Ankush",data)

// and your age :22
// Hey this is a Ankush