JavaScript
Different ways to define a function on JavaScript
Here we're going to talk a little bit about different ways to declare a function and JavaScript
Function Declaration:
function add (num1, num1){
return num1 + num2
}
console.log(add(1,2))
Anonimous function:
let add = function (num1,num2){
return num1 + num2
}
console.log(add(3,4))
The difference between function declaraton and
function expressions:
The two functions run the same way except with one edge case "Hoisting"
Because JS reads all the script before runs the script we can call a function before declare it
using function declaration (Hoisting)
"It already knows that I have a declared function after I call it"
When we use function expressions (functions assigned to a variable)
Because JS knows that there's a variable, but it doesn't know if it's a function or a string and it
doesn't know what is the value of that variable;
Structuring a code in JavaScript
// Variables
let name = "lucas"
// Methods & Functions
function callName () {
console.log(name)
}
// Inits & Event Listeners
callName()
Arrow Functions:
// a traditional function
let addTraditional = function (num1, num2){
num1 + num2;
console.log(add(3,4))
}
// an arrow function version
let add = (num1, num2) => {
return num1 + num2;
}
console.log(add(3,4))
The difference between a expression function and an
arrow function:
In arrow function there's no use of word function