javascript function declaration types
What are JavaScript functions?
Functions are essential building blocks in JavaScript, they are packed with logic for performing tasks. A function can be likened to a procedure that performs a task or calculates a value. However, for this procedure to be considered as a function, it needs to take some input and return an output. For instance, a function to calculate a value should take the user’s input (value to be calculated) and return an output (calculated value). In simple terms, a function is a group of statements that perform a task.
One thing you will hear often when getting into JavaScript is that functions are "first-class citizens".
JavaScript functions are values in themselves, and they can be stored in variables and passed into other functions.
What is a value in JavaScript?
We know that in the examples below 👇 true and 100 are values.
Types of function in javascript
1. Function Expression
2. Anonymous Functions
3. Immediately Invoked Function Expression
4. Constructor Functions
5. Getter Functions
6. Hoisting
7. Arrow Functions
Function Declarations:
Earlier in this tutorial, you learned that functions are declared with the following syntax:
function functionName(parameters) {
// code to be executed
}
Function Expressions:
A JavaScript function can also be defined using an expression.
A function expression can be stored in a variable:
Example:
const x = function (a, b) {return a * b};
No comments:
Post a Comment