
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to load a JavaScript function using the variable name?
In this tutorial, we will learn to load a JavaScript function using the name of a variable.
Functions are the block of statements that takes an input and displays the result to the user after an execution. We can use these blocks of codes repeatedly by only declaring the function, and these functions can help a programmer in many ways as it also reduces efforts.
JavaScript also supports the use of functions like other programming languages. Functions in JavaScript can be either built-in or user-defined.
There are different ways to declare a function and call it. Generally, a simple Function uses its name to call itself anywhere in the program. But there are also other ways to call a function. Let's look to load a JavaScript function using the name of a variable.
Following is the type of function that allows us to load a JavaScript function using the name of a variable:
- Anonymous function
The Anonymous Function
Generally, we assign a name for a function. The anonymous function is the function without a name with it. We only use the function keyword with parenthesis to declare the Anonymous function without adding a name.
We cannot access the Anonymous function by just declaring it. We have to store this function as a value in a variable. And then, we can call functions using that variable. Users can follow the syntax given below to use the Anonymous function to load a JavaScript function using the name of a variable −
Syntax
function() { // Function Body } //Using Arrow function var var1= ()=>{ //Function Body }; //Calling function var var_name=function() { // Function Body }; var_name();
Example 1
In the example, we have used the Anonymous function to load a JavaScript function using the name of a variable.
<html> <body> <p> Use <i> Anonymous function </i>to load a JavaScript function using the name of a variable. </p> <div id="div1"></div> <script> var anonymous = function() { return "This is anonymous function"; }; document.getElementById("div1").innerHTML = anonymous(); </script> </body> </html>
In the output, you can see that we have used the Anonymous function to load a JavaScript function using the name of a variable.
Example 2
In this example, we have used the Anonymous function using the arrow function to load a JavaScript function using the name of a variable.
<html> <body> <div id = "div1"></div> <script> var divide = ()=> { return 34/12; }; document.getElementById("div1").innerHTML = divide(); </script> </body> </html>
Here you can see we have used the Anonymous function with an arrow function to load a JavaScript function using the name of a variable.
Example 3
In the example, we have used the Anonymous function to load a JavaScript function using the name of a variable by clicking a button.
<html> <body> <p> Click the "Click here" button to execute anonymous fucntion </p> <button onclick = "anonymous()">Click here</button> <div id="div1"></div> <script> var element = document.getElementById("div1"); var anonymous = function() { var new_element = document.createElement('p'); var text = document.createTextNode("Executing Anonymous function on click"); new_element.appendChild(text); element.appendChild(new_element); }; </script> </body> </html>
In the output, you can see that we have used the Anonymous function to load a JavaScript function using the name of a variable on clicking a button. We have printed a message on the screen by clicking a button.
We have learned to use an anonymous function, using which we can load a JavaScript function using the name of the variable. We have also used the arrow function to declare an Anonymous function.
- Related Articles
- How to execute a JavaScript function using its name in a variable?
- How to add a property to a JavaScript object using a variable as the name?
- How to ignore using variable name as a literal while using push() in JavaScript?
- How to change the value of a global variable inside of a function using JavaScript?
- How to define global variable in a JavaScript function?
- How do I call a JavaScript function on page load?
- How can we assign a function to a variable in JavaScript?
- How to call a function from its name stored in a string using JavaScript?
- Assigning function to variable in JavaScript?
- How to load a JavaScript file Dynamically?
- How to define a JavaScript function using Function() Constructor?
- How to define a JavaScript function using the expression?
- How to delay a JavaScript function call using JavaScript?
- Extract the value of the to a variable using JavaScript?
- How to use variable number of arguments to function in JavaScript?
