Named function:
- A named function has an identifier (name) after the
functionkeyword. - Easy to debug the errors because errors show the function name.
- You can call the function before it is defined.
- You can call it anywhere in the code for execution and reuse.
- It is hoisted.
function add(a, b) {
return a + b;
}
Anonymous function:
- An anonymous function has no name.
- Usually it is assigned to a variable or used as a callback function.
- hard to debug because stack traces show “anonymous”.
- Commonly used with arrow functions (()=>{}).
- It is not hoisted.
const add = function(a, b) {
return a + b;
};
Top comments (0)