JavaScript: Function Types

Let's start it and see some basic types!



 Named Function (Traditional way): The traditional way of creating a function and it's a set of statements that performs a task or calculates a value! 


function sayHello() {
  console.log('Hey everyone!');
}
sayHello();
// Output
'Hey everyone!'


 Arrow Function: 


Arrow Functions are simpler, are always unnamed and compact than traditional function expression!


const sayHello = () => console.log('Hey everyone!');
sayHello();
// Output
'Hey everyone!'


 Anonymous Function:

Anonymous Functions don't have a name specified after the function declaration, so we declare it with a variável to call this function at a some point and the function is anonymous but assigned to a variable!


const sayHello = function () {

  console.log('Hey everyone!');
}
sayHello();
// Output
'Hey everyone!' 


◉ Higher Order Function:


Higher Order Functions in a nutshell words is a function that can take one or more functions as arguments or return a function as output. Some of Higher order types is like: reducefiltermap and others.


// A simple function to print a console.log
function sayHello(){
  console.log('Hey everyone!');
}
// Higher Order Function Example:
function higherOrderFnExample(functionToExecute){
  console.log('I can do anything!');
  functionToExecute()
}
higherOrderFnExample(sayHello);


 Constructor Function:


It's is used to create Function objects and we need to use the new keyword to create a new Function instance!


// Creating the Constructor Function

function Car () {

  this.name = 'Ford',

  this.color = 'Black'

}

// Creating the Object

const myCar = new Car();

console.log(myCar.name);

// Output

'Ford'

console.log(myCar.color);

// Output

'Black'


Good Luck !!


𝐋𝐢𝐤𝐞

𝐒𝐡𝐚𝐫𝐞

Tags