- Function Statement aka Function Declaration:
The syntax of writing function is called as Function Statement or Function Declaration.
function fooBar() {
console.log("Hello World!")
}
- Difference between Function Parameters and Arguments
If we add variables inside round brackets in Function Declaration then those variables are called as Parameters, and they are copy of the sent variable through function call.
If we call the function and along with it we pass a variable inside round brackets of function call then those variables are called as Arguments.
πNote:- We can keep the names of parameter and argument same, but to avoid confusion we usually name it differently.
// Parameter
function fooBar(parameter) {
console.log("Hey Foo Bar, it's me ", parameter)
}
// Argument
let argument = "John Doe"
fooBar(argument)
- Anonymous Function
When we create a function but without name is called as Anonymous Function.
setTimeout(function() {
console.log("Hello World!")
})
- Function Expression
It is almost same as creating Anonymous Function but not only creating it, also assigning it to a Variable means Function Expression. After assigning the function to the variable, the function act as a value and it is not added into the Global Scope. We can also create a Named Function and assign it to a variable then it is called as Named Function Expression.
// Function Expression
let foo = function() {
console.log("Hello World!")
}
// Named Function Expression
let bar = function foo() {
console.log("Hello World!")
}
- First Class Functions
Functions has ability to be used like Values is called as First Class Functions. Functions can be returned as values from a function or can be send as arguments to a function. First Class Functions are also known as First Class Citizens.
// Function returned as Value
function foo() {
return function() {
console.log("Hello World!")
}
}
let helloWorld = foo()
// Functions sent as Argument
function foo(funcParam) {
bar()
console.log("Here we are in Foo!")
}
function bar() {
console.log("Hello World!")
}
foo(bar)
Thanks for reading, will cover Arrow Functions in next blog π
Top comments (0)