Javascript Functions Types

Photo by Markus Spiske on Unsplash

This is the most commonly used way, we create a binding to assign a function as a value. In these cases, we can use this function like any other binding, allowing us to pass the function to other functions and so on, also we can assign a new value, went if not constant.

const square = function(x) {
return x*x;
}
console.log(square(12))
// --> 144

This is a short way to create a function binding, we use the function keyword at the start of a statement. The main key difference is when you call these functions because they are conceptually moved to the top of the scope, in consequence, we can call the function before it’s have been defined.

console.log("The future says:", future());function future(){
return "You'll never have flying cars";
}

This last way looks very different from the others. In this case, instead of the function keyword, it uses an arrow (=>) made up of an equal sign and a greater-than character, for example:

const square = (X) => { return x*x };
//also you can do this
const square = X => X * X; // implicit return, we remove () because we have just one paremeter.

If you like to get deep, check this out Eloquent Javascript functions.

Passionate reading || Coding Addicted || FullStack Web Developer from Costa Rica

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store