Function Declaration in JavaScript
Explore various JavaScript function declaration methods, such as function declarations, expressions, generator functions, and arrow functions. Understand their syntax, usage, and how each affects the function's behavior and interaction with scope. This lesson prepares you to write reusable functions essential for React development.
A function is a piece of code which is defined only once but can be called a countless number of times. A JavaScript function comprises several components which affect its behavior. A typical JavaScript function has the following components:
- the
functionkeyword - the name
- the parameter(s)
- the returned value
- the return type
- the context
this
Did you know? In JavaScript, functions are actually objects. Just like any typical object, they have attributes and methods too. The only thing that differentiates them from objects is that they can be called
What’s the difference between Named and Anonymous function?
The only difference is that Anonymous Functions are declared at runtime, means they are defined and called at the same time. The reason why they are called Anonymous is that they are not given a proper name before compilation. A typical way to declare a function in JavaScript is:
Now here’s how an anonymous function is ...