DEV Community

Cover image for JavaScript: Function Types You Should Know!
Renan Ferro
Renan Ferro

Posted on

JavaScript: Function Types You Should Know!

Heyy, how are you?!

Have you ever had to deal with some JavaScript functions just... they weren't exactly the way you expected? Maybe it looked like the functions were written in a different way than what you've seen and I don't mean "logically" speaking... ๐Ÿ˜…

If yes, I would like to introduce you to some possibilities of types of functions that we can find!

So, 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!'
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ 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!'
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ 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!'
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ 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: reduce, filter, map 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);
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ 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'
Enter fullscreen mode Exit fullscreen mode

Hope this makes you feel a bit more comfortable with functions and their possibilities!

Feel free to reach out to me if you have any questions!

and obviously I hope you enjoyed it ๐ŸคŸ๐Ÿ’ช๐ŸคŸ๐Ÿ’ช

Top comments (16)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

Did you know that the act of assigning an anonymous function to a variable makes that function no longer anonymous (in most cases)?

This code:

function add(a, b) {
  return a+b
}
Enter fullscreen mode Exit fullscreen mode

Is equivalent to this code:

let add = function(a, b) {
  return a+b
}
Enter fullscreen mode Exit fullscreen mode

BOTH functions actually have a name - so neither is anonymous. You can test this by checking add.name - which in both cases above will be 'add'.

You can find more about anonymous functions in this post.

Collapse
 
renancferro profile image
Renan Ferro

Yess, is not anonymous because it is assigned to a variable ๐Ÿ˜…! Thanks for the tip and I'll definitely read it!!

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

Just because a function is assigned to a variable does not necessarily mean it is not anonymous. It is perfectly possible to assign an anonymous function to a variable without losing its anonymity:

const a = []
a.push(function(a, b) {
  return a+b
})
let myFunction = a[0]

console.log(typeof myFunction)  // 'function'
console.log(myFunction.name)  // <empty string>
Enter fullscreen mode Exit fullscreen mode

The name of the variable and the name of the function are two different things:

let myFunction = function add(a,b) {
  return a+b
}
console.log(myFunction.name)  // 'add'
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
renancferro profile image
Renan Ferro

Niiiicee ๐Ÿคฉ

Collapse
 
prsaya profile image
Prasad Saya

I'd like to mention the IIFE (Immediately Invoked Function Expression) here. This is a JavaScript function that runs as soon as it is defined. See IIFE @ MDN .

Collapse
 
renancferro profile image
Renan Ferro

Cool, I like the IIFE functions! We have Pure Functions too, for example! But in this article I just wanted to share some basic types! But thanks for the comment and the tip.

Collapse
 
whatsavadim profile image
WhatsAVadim • Edited

I think you could add potential use cases for them. Specifically maybe mention async await and which functions can/cannot use this. Additionally how some js methods mixed with arrow functions throw errors. Besides that, good stuff :)

Collapse
 
renancferro profile image
Renan Ferro

Sure, I'm doing a new article talking about async/await, Promise and some other stuff! This article I just wanted to talk a little bit about some different types functions that we can use with JavaScript.

But thanks a lot for the tip ๐Ÿ˜€

Collapse
 
dimensioncloud profile image
DimensionCloud

can u revised my code ?

Collapse
 
renancferro profile image
Renan Ferro

Hey, what code?! ๐Ÿค”

Collapse
 
dimensioncloud profile image
DimensionCloud • Edited

express.js ? on my website: dimense.net code: github.com/reroote/core

Collapse
 
dhruvjoshi9 profile image
Dhruv Joshi

Informative!

Collapse
 
renancferro profile image
Renan Ferro

Thanks dude ๐Ÿฅณ

Collapse
 
jonahbutler profile image
jonah-butler

I think it's worth mentioning too: high order functions are the basis of Decorators. For those not working with Angular or NestJS regularly, that syntax can be a little odd at first. But working with HOF will make the decorator syntax and use case very clear.

Collapse
 
bsorrentino profile image
bsorrentino

I think you should add generator functions

Collapse
 
fruntend profile image
fruntend

ะกongratulations ๐Ÿฅณ! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up ๐Ÿ‘