Function Declarations
๐จโ๐ผ We need a way to group code into reusable units. Function declarations let
us define named functions that we can call whenever we need them.
The syntax:
function functionName() {
// code to run when called
}
// Call the function
functionName()
- Create a function called
greetthat logs "Hello, world!" - Create a function called
sayGoodbyethat logs "Goodbye!" - Call both functions
๐ฐ Example:
function celebrate() {
console.log('๐ Hooray!')
}
celebrate() // Logs: ๐ Hooray!
celebrate() // Logs: ๐ Hooray! (again!)
๐ Functions on MDN