In any scripting or programming language, functions play a major role in the construction of simpler to complex programs. In Javascript there are three types of functions, based on the way it is structured as well as on the kind of usage.
What are the those three essential functions?
Here comes the,
Named Functions
//Function Definition
function printNamenTimes(name,n)
{
for (i = 0; i < n; i++)
{
console.log(name);
}
}
//Function Call
printNamenTimes("Apple",10);
A Named Function as the name implies, has a name associated with it in the definition of the function and the function can be called easily with its name along with the arguments passed to it, whenever we need it in the program. This is a very straightforward way of constructing a function in Javascript.
Useful: When we need to call a function many times within a particular script.
Lets see the next one...
Anonymous Functions
//Function Definition
var nameprinting = function(name,n)
{
for (i = 0; i < n; i++)
{
console.log(name);
}
}
//Function Call
nameprinting("Orange",10);
Anonymous functions as the name indicates are anonymous and it has no name identification.
As you can see the function expression is assigned to a variable named "nameprinting"
and the variable is considered and called as a function, as it holds the function expression withing itself.
Useful: This form of function is mostly used when the function has single instance usage in the entire script.
Immediately invoked functions
//Function Definition
var nameprinting = (function(name,n)
{
for (i = 0; i < n; i++)
{
console.log(name);
}
return name;
})("Grapes",10);
//printing the variable returns value "Grapes"
console.log(nameprinting);
The structure of the immediately invoked function, is in a way that the parameters are passed in the function expression itself. In immediately invoked functions, the browser executes the function immediately after getting into the function. The variable "nameprinting" which has got initially the function expression, eventually stores the return value of the function within itself and can be used outside the function expression.
IIFE(Immediately Invoked Function Expression) is a very powerful function pattern, that can be used in more complex javascript programs, which is out of the scope of this particular post, and will be covered separately.
What are the those three essential functions?
- Named Functions
- Anonymous functions
- Immediately invoked functions
Here comes the,
Named Functions
//Function Definition
function printNamenTimes(name,n)
{
for (i = 0; i < n; i++)
{
console.log(name);
}
}
//Function Call
printNamenTimes("Apple",10);
A Named Function as the name implies, has a name associated with it in the definition of the function and the function can be called easily with its name along with the arguments passed to it, whenever we need it in the program. This is a very straightforward way of constructing a function in Javascript.
Useful: When we need to call a function many times within a particular script.
Lets see the next one...
Anonymous Functions
//Function Definition
var nameprinting = function(name,n)
{
for (i = 0; i < n; i++)
{
console.log(name);
}
}
//Function Call
nameprinting("Orange",10);
Anonymous functions as the name indicates are anonymous and it has no name identification.
As you can see the function expression is assigned to a variable named "nameprinting"
and the variable is considered and called as a function, as it holds the function expression withing itself.
Useful: This form of function is mostly used when the function has single instance usage in the entire script.
Immediately invoked functions
//Function Definition
var nameprinting = (function(name,n)
{
for (i = 0; i < n; i++)
{
console.log(name);
}
return name;
})("Grapes",10);
//printing the variable returns value "Grapes"
console.log(nameprinting);
The structure of the immediately invoked function, is in a way that the parameters are passed in the function expression itself. In immediately invoked functions, the browser executes the function immediately after getting into the function. The variable "nameprinting" which has got initially the function expression, eventually stores the return value of the function within itself and can be used outside the function expression.
IIFE(Immediately Invoked Function Expression) is a very powerful function pattern, that can be used in more complex javascript programs, which is out of the scope of this particular post, and will be covered separately.
No comments:
Post a Comment