
- 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
Assigning function to variable in JavaScript?
Use return statement to assign function to variables. Following is the code −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <style> .demo { background: skyblue; height: 20px; width: 75%; margin: 10px; padding: 10px; } </style> </head> <body> <div class="demo" data-st="John">John Smith</div> <div class="demo" data-st="David">David Miller</div> <div class="demo" data-st="Adam">Adam Smith</div> <script> var studentNames = function() { var names = []; $(".demo").each(function() { names.push($(this).data('st')); }); return names.join(); }; console.log( studentNames() ); </script> </body> </html>
To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
Output
This will produce the following output −
- Related Articles
- Assigning values to a computed property in JavaScript?
- How to define global variable in a JavaScript function?
- Extracting only date from datetime field in MySQL and assigning it to PHP variable?
- How to use variable number of arguments to function in JavaScript?
- How can we assign a function to a variable in JavaScript?
- Setting a default variable value to undefined in a function - JavaScript?
- How to load a JavaScript function using the variable name?
- How to execute a JavaScript function using its name in a variable?
- Assigning Values to Variables in Python
- Assigning arrays in Java.
- How can I check if a JavaScript variable is function type?
- Assigning packages to delivery unit in SAP HANA
- Variable Hoisting in JavaScript
- How to change the value of a global variable inside of a function using JavaScript?
- Most Profit Assigning Work in C++

Advertisements