Sunday, February 16, 2020

Functions In JavaScript

Functions In JavaScript


Function


Functions are the block of code/lines used to perform some defined tasks. Functions can be executed when invoked by something. The functions in JavaScript are declared using the "function" keyword. We can also create a function using a Function constructor. The functions are very much like Procedures or Subroutines in many other programming languages.

Functions are useful in programming for code reuse, as the same code can be used at multiple places by invoking it by its name. A function provides some output to any given input.

JavaScript has first-class functions. We can pass a functions as an argument to any other function or even a function can return a function. They can be assigned to other variables as values. A function is an object like other objects in JavaSript such as String, Array, etc.


The syntax for the function 


function function_name(arg1, arg2,..., argN){
   statement/s
}

for example

<html>
    <head><title>Function Demo</title></head> 
<body bgcolor='yellow'>
<script>
   function showDate()
      {   
         d=new Date();
         document.write(d);
}
</script>

<input type='button' value='show date' onclick='showDate()'/>
</body>
</html>

The name of the function


function_name is the name of the function. We can invoke this function using its name later. The name of a function may contain letters, digits, underscores, and dollar signs. 

The parameters(optional)


The function may also carry parameters within the (...) parenthesis. The number of parameters can vary from 0 to n, where generally varies according to JavaScript engine. These parameters work like local variables in the body of a function. The executable block(body of the function) of code in the function is kept within {...} brackets. 

The "return" statement


A function may return some value, the execution of the function is terminated after the return statement. This value is returned to the caller.
For example,

function isEven(num)//only for num>=0
     {
         if(num%2==1)
         {
         return false;
          }
         else
         {
         return true;
         }
      }


The Function in Expressions(anonymous function)


A function can be declared as an expression, this type of function is known as the anonymous function.

var num=function(x,y) //parmeters
              {
               return x+2*y;
              }

var n=num(7,8);

The Function Constructor


We can also use a Function constructor to create a function.

var num=new Function(x, y, return x+2*y);
var n=num(7,8);

The Self-invoking Function 


In JavaScript, we can declare a self-invoking function, it means we do not need to explicitly invoke them.

(function() 
              {
                  document.write("Welcome to JavaScript");
              })();


JavaScript Arrow Function


JavaScript arrow functions are like lambda functions in Python or blocks in Ruby. JavaScript arrow functions are anonymous functions having special syntax. Read the article on JavaScript Arrow functions.

var b = () => {
  return "This is arrow function";
}


Functions example