Functions are the basic building blocks of the Javascript. A function in Javascript is a code block that performs a specific task or set of tasks.
There are basically 4 (four) different ways we can define functions in Javascript.
- Function Declaration
- Function Expression
- Arrow Function Expression
- Concise Arrow Function Expression
// Function Declaration
function square(x) {
return x * x;
}
// Function Expression
const square = function(x) {
return x * x;
}
// Arrow Function Expression
const square = x => {
return x * x;
}
// Concise Arrow Function Expression
const square = x => x * x;
If you prefer you can take a look at the video tutorial,
Top comments (2)
Thank you! Nice writeup!
Would you mind, elaborate a bit?
Where are the differences besides different ways of declaration?
What is the scope / when could it be referenced?
If you use a function expression, does hoisting play into that?
Thank you for this post!
If you put "javascript" term after the code delimiter the js syntax will be highlighted, like that: