JavaScript Functions
A function is a block of code that is executed when it is called. Functions are used to encapsulate code and make it reusable.
JavaScript Function Syntax
The syntax for a JavaScript function is as follows
function functionName(parameters) {
// code to be executed
}
The functionName is the name of the function. The code to be executed is the body of the function.
JavaScript Function Parameters
Parameters are values that are passed to a function when it is called. Parameters are used to pass data to the function.
JavaScript Function Calls
A function is called by using its name. The syntax for calling a JavaScript function is as follows:
functionName(parameters);
JavaScript Function Return Values
A function can return a value. The value that is returned by the function is the result of the function’s execution.
Here is an example of a JavaScript function:
This function takes two arguments or parameters: start
and end
. The start
argument is the first number in the range, and the end
argument is the last number in the range. The function returns the sum of all the numbers in the range, from start
to end
which is console logged.