There are 3 ways which functions can be declared in JavaScript.
In this article, I'm going to show you and compare these.
Named Functions
This type receives a name for the function declaration:
function myFunction(text) {
console.log(text);
}
myFunction('Hello World!');
Anonymous Functions
This type doesn't receive explicitly any name for declaration. Instead, the function will be assigned to a variable for invoking.
const myFunction = function(text) {
console.log(text);
}
myFunction('Hello World!');
Arrow Functions
This type is similar to lambda functions in other programming languages. It doesn't receive explicitly any name (it will be assigned to a variable for invoking) and the structure is so simpler.
const myFunction = (text) => {
console.log(text);
}
myFunction('Hello World!');
BOOM! Now you've been learning 3 types of function declaration in JavaScript which may help you a lot in developing web applications and etc.
You can or may want to connect with me through the networks I've put on my website: Ali Bahaari's Website
Top comments (4)
In the case of the first, this is not really advisable. You see, saying
JavaScript parses and does this:
Why might that be a bad thing? Because
var
will hoist, and usually to the global scope. The AitBnB style guide does a great job of explaining why this is an antipattern.Thanks for sharing this idea!
Perfect! I love it!
I appreciate that.