JavaScript Functions
A function is an event-driven or reusable block of code that executes when it is invoked;
JavaScript function syntax
A function is a block of code wrapped in curly braces, preceded by a keyword function:
When the function is called, the code inside the function is executed.
A function can be called directly when an event occurs (such as when a user taps a button) and can be called from anywhere by JavaScript.
Calling a function with parameters
When you call a function, you can pass a value to it, which is called a parameter.
These parameters can be used in functions.
You can send any number of arguments separated by commas (,):
When you declare a function, declare the argument as a variable:
Variables and parameters must appear in a consistent order. The first variable is the given value of the first parameter passed, and so on.
function with return value
Sometimes, we want the function to return the value to the place where it was called.
This can be achieved by using the return statement.
When you use the return statement, the function stops execution and returns the specified value.
Because local variables are only used within functions, different functions can use variables of the same name.
Local variables are created when the function starts executing, and local variables are automatically destroyed when the function finishes executing.
If a variable is not declared within a function (without using the var keyword), the variable is a global variable.
The JavaScript variable life cycle is initialized when it declares.
Local variables are destroyed after the function has finished executing.
Global variables are destroyed after the page is closed.
JavaScript Function Learning 3 (RPM)