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!)