Function in JavaScript
Function is a very interesting thing available in any programming language. if you want to fall in love with JavaScript, you need to understand that how functions behave in JavaScript. This article will give you brief understanding about why actually we need functions? and how function are used to make your code more flexible, readable, optimized.
Whatever programming language you come across, documentation always recommend you to use functions, let’s understand actual need of Function:
Function is a kind of code which is required to use multiple times in your program so rather than writing the same code again and again, we can simply make a function or you can save block of code and whenever there is code requirement, we can simply call that function or code, by following this, we are not just optimizing our code but also making it efficient, reusable and production ready.
Every function in JavaScript is a type of object. let’s understand how a function is define and use in JavaScript.
function sample(a){return a*a;}
Above we have define the function ,it is known as ‘function definition’. It is starting with a keyword ‘function’ and once you done with a definition you need to call your function[anywhere in program].If you run this code you will found nothing in output because we are not calling this function so let’s call it first
console.log(sample(10)); //This is function callingoutput:100
let’s understand how this function is returning 100 as output
When we call sample(10),we are simply calling the function and providing value to a. Here when we passing value at the time of function calling is known as argument and while defining the function we are passing a placeholder which is known as parameter. When we are calling function we are simply calling the whole code written in the function definition and once the function complete it’s task, called function will be removed from the memory.
We have written return statement which is give square of a given number that’s why we are getting 100, it is up to the programmer what he want to return from a function if there is no return statement in function then it will give you undefined and you can also use as many return as you want JavaScript interpreter will give first return and remove function from the memory irrespective of how many return are there. As we are consoling the return value that’s why we are getting 100 in console.
I hope you understood basic nature of function, don’t worry we will also dip dive function behavior soon.
We can pass two types of value in function argument
(1)Pass by value
(2)Pass by reference
Example that we seen is actually pass by value, let’s now check pass by reference
There are several style in which you can define a function.
A function expression is similar to and has the same syntax as a function declaration
Immediately invoked function expression(IIFE):Whenever you want a function that call once throughout the program you can create it as a IIFE function. IIFE are the function expression that are invoked as soon as function is defined so you need not to call it explicitly. Let’s see how we can declare IIFE function
(function(){console.log(‘I am IIFE’);})();//output: I am IIFE
Generator function expression:
function* (parameter){Statements….}//this is also a example of Anonymous function
Use case of Generator function: You can have better control over the function execution, you can decide when to halt a function and when to resume it from the halted position. If you are not able to understand Generator function, don’t worry about this we will cover this function separately, for now just focus how it is declared.
Arrow function: Arrow functions were introduced in ES6.
sample=(a)=>{
return a*a;}
console.log(sample(10));
Function constructor: you can create function with using Function( ) constructor along with the new operator but it is not recommended.
var sample= new Function(“a”, “b”, “return x*y;”);
Before you leave keep these point noted:
(1) Function is a type of Object in JavaScript.
(2) Function is a non-primitives datatype.
(3) Function is useful whenever we need a code repetition.
(4) There are many way to declare a function for example arrow, IIFE etc.
(5) If you don’t provide return type, function will return undefined.
Conclusion: Functions are the heart of JavaScript that’s why it’s very crucial to learn them well. In the next article, we will explore these functions types in detailed.