A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). The code to be executed, by the function, is placed inside curly brackets_{}_. Function parameters are listed inside the parentheses () in the function definition.Function arguments are the values received by the function when it is invoked.Inside the function, the arguments (the parameters) behave as local variables.
- When an event occurs (when a user clicks a button).
- When it is invoked (called) from JavaScript code.
- Automatically (self invoked).
When JavaScript reaches a return statement, the function will stop executing.
- You can reuse code: Define the code once, and use it many times.
- You can use the same code many times with different arguments, to produce different results.
- A well-named function communicates clearly it's purpose.
- Functions help us avoid repeated code.