Skip to content

Latest commit

 

History

History
64 lines (46 loc) · 1.13 KB

File metadata and controls

64 lines (46 loc) · 1.13 KB

Funcions

why do you need functions in your application in its simplest term a function is a custom complex type you can create on your own to:

  • add custom functionlity
  • create a new tye of data
  • manipulate data
  • group related functionalities

example:

    let a=2,
        b= 3

    const sum = a+b
    console.log(sum) // 5 
    
    // introduce a function:
    function add(a,b){
        return a+b
    }

    console.log(add(4,5)) // 9
    console.log(add(7,5)) // 12
    console.log(add(17,5)) // 22

Types of function

named functions

they have the keyword function in most cases, and they are hoisted by default. they can be used before being defined

add(500,600,809)
function add(a,b,c){
    console.log(a+b+c);
}

variadic: they take arguments

function add(a,b,c){
    console.log(a+b+c)
} // a variadic function that takes 3 arguments - a,b,c

non variadic: they do not take arguments

function add(){
    let a,b,c = 3,4,6
    console.log(a+b+c)
}

Anonymous functions ( Arrow functions)

you cannot use them before definition