How To Use JavaScript Function as Variable

How To Use JavaScript Function as Variable

JavaScript Function can be used as a variable almost everywhere, in assignments, formulas and calculations.

JavaScript Function

A JavaScript function is a section of code made to perform same task repeatedly, so you don't have to write same set of lines again and again. JavaScript Functions make your code reusable.

JavaScript Function Example

function myFunction(a, b) 
{
  return a + b;   // This function returns the sum of a and b
}

This is simple example of JavaScript Function that adds two numbers and return the sum.

JavaScript Function Syntax

JavaScript Function is defined using the keyword function. After that function name is written, followed by parentheses. Inside these parentheses parameters are written, separated by commas. After that, the block of code is written inside the curly brackets.

JavaScript Function as Variable

First consider a simple example of JavaScript Function which calculates the area and returns the result.

We have used simple variables in this code.

function calculateArea(length , width)
{
var area = length * width;
return area;  // function return
}
var area = calculateArea(10 , 5);
var roomArea = "The room area is " + area; // with variable

Now we will use JavaScript Function as a variable. See code example below.

function calculateArea(length , width)
{
var area = length * width;
return area;  // function return
}
var roomArea = "The room area is " + calculateArea(10 , 5); // function being used as a variable

In the above code JavaScript Function calculateArea() is being used as variable in place of variable area. You can also use any JavaScript Function as variable in your code.

Count Number of Rows in Table using JavaScript Move Background Image with Mouse Using JavaScript Disable Text Selection Using JavaScript Get Selected Radio Button Value On Click using JavaScript Get input Element Value On Click Using JavaScript Get Class of Clicked Element Using JavaScript Toggle Text of HTML Element with JavaScript onclick Call Multiple Functions on one click using JavaScript onclick Check UnCheck all Checkboxes with JavaScript Open Select File Dialog Box Using JavaScript Display Message When File Is Selected Using JavaScript Get Current Page URL using JavaScript Get ID of Clicked Element using JavaScript How To Delay JavaScript Function Call Change Background Color onclick using JavaScript Get Clicked HTML Element Name Using JavaScript Delete HTML Element using JavaScript Change HTML Attribute Value using JavaScript Show and Hide Element On Click Using JavaScript Passing JavaScript Value from one Page to another T-Rex Run Game Hacked - Hack T Rex Run Game How To Use JavaScript Function as Variable Display Current Date and Time Using HTML and JAVASCRIPT Get Unicode Value of Pressed Keyboard Key using JavaScript Detect Enter Key Press Using JavaScript Hide and Show Element on Enter Key Press Get Mouse Cursor Position in Pixels using JavaScript Open Link in New Window using JavaScript Open Link in New Tab using JavaScript Get Image Width and Height using JavaScript Remove Last Digit of Number using JavaScript Bitwise OR Simple JavaScript Calculator Using Vanilla JavaScript Disable Right Click on Website Using JavaScript How To Add Line Breaks in JavaScript Alert Box