FUNCTIONS:
=>Function is a block of code that executes the code when the function (function name) is called.
=>Functions are reusable code blocks designed for particular tasks.
Why Use Functions?
Functions help you to:
- Reuse code (run many times).
- Organize code into smaller parts.
- Make code easier to read and maintain.
To run a function, you call it by using its name followed by parentheses like sayHello():
Ref: W3 School.
Function Scope:
=> The functions that are executed within the {}( brackets)
The return Statement:
When a function reaches a return statement, the function stops executing.
The value after the return keyword is sent back to the caller.
Example:
Parameters and Arguments
In JavaScript, function parameters and arguments are distinct concepts:
- Parame****ters are the names listed in the function definition.
- Arguments are the real values passed to, and received by the function.
Ref: W3 School
The argument 4 is assigned to the parameter a.
The argument 5 is assigned to the parameter b.
Arrow Function Syntax
- Arrow function symbol (=>)
- We can remove the word function, the curly brackets and the return keyword.
Function to compute the product of a and b:
After useing Arrow function: