Function Declaration in JavaScript
A short introduction to JavaScript functions, the number of ways it can be declared in JavaScript and a few concepts related to it.
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
function
keyword - 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:
Get hands-on with 1200+ tech skills courses.