Saturday, September 21, 2019

What is Function in JavaScript?

JavaScript functions are used to perform operations. We can call JavaScript function many times to reuse the code.

Function is a block of statement through which can perform a specific operation.

Advantage of JavaScript function

There are mainly two advantages of JavaScript functions.
  1. Code reusability: We can call a function several times so it save coding.
  2. Less coding: It makes our program compact. We don’t need to write many lines of code each time to perform a common task.

JavaScript Function Syntax

The syntax of declaring function is given below.

  1. function functionName([arg1, arg2, ...argN]){  
  2.  //code to be executed  
  3. }  
JavaScript Functions can have 0 or more arguments.

JavaScript Function Example

Let’s see the simple example of function in JavaScript that does not has arguments.
  1. <script>  
  2. function msg(){  
  3. alert("hello! this is message");  
  4. }  
  5. </script>  
  6. <input type="button" onclick="msg()" value="call function"/>  

JavaScript Function Arguments

We can call function by passing arguments. Let’s see the example of function that has one argument.
  1. <script>  
  2. function getcube(number){  
  3. alert(number*number*number);  
  4. }  
  5. </script>  
  6. <form>  
  7. <input type="button" value="click" onclick="getcube(4)"/>  
  8. </form>  

Output of the above example

Function with Return Value

We can call function that returns a value and use it in our program. Let’s see the example of function that returns value.
  1. <script>  
  2. function getInfo(){  
  3. return "Hello Santosh! How r u?";  
  4. }  
  5. </script>  
  6. <script>  
  7. document.write(getInfo());  
  8. </script>  

Output of the above example

Hello Santosh! How r u?

JavaScript Function Object

In JavaScript, the purpose of Function constructor is to create a new Function object. It executes the code globally. However, if we call the constructor directly, a function is created dynamically but in an unsecured way.

Syntax

  1. new Function ([arg1[, arg2[, ....argn]],] functionBody)  

Parameter

arg1, arg2, .... , argn - It represents the argument used by function.
functionBody - It represents the function definition.

JavaScript Function Methods

Let's see function methods with description.
MethodDescription
apply()It is used to call a function contains this value and a single array of arguments.
bind()It is used to create a new function.
call()It is used to call a function contains this value and an argument list.
toString()It returns the result in a form of a string.

JavaScript Function Object Examples

Example 1

Let's see an example to display the sum of given numbers.
  1. <script>  
  2. var add=new Function("num1","num2","return num1+num2");  
  3. document.writeln(add(2,5));  
  4. </script>  

Output:
7

Example 2

Let's see an example to display the power of provided value.
  1. <script>  
  2. var pow=new Function("num1","num2","return Math.pow(num1,num2)");  
  3. document.writeln(pow(2,3));  
  4. </script>  

Output:
8

No comments:

Post a Comment