How to get the javascript function parameter names/values dynamically?
Given any arbitrary JavaScript function and the task is to return the parameter names of the function.
Approach: JavaScript contains a method called Function.toString() which is used to represent a function code into its string representation. This method is used to get the parameter names/values.
- First, get the function’s code to its string equivalent using toString() method.
- Then remove all the unnecessary codes like comments, function body, white spaces and ES6 arrow (if any).
- Identify the first occurrence of ‘(‘, it will be just before the starting of parameters.
- The last character of the string will be ‘)’ which remove all comments, function body, white spaces, and ES6 arrow.
- Also, the last character will be just after the end of the parameters.
Example:
<script> // JavaScript program to get the function // name/values dynamically function getParams(func) { // String representaation of the function code var str = func.toString(); // Remove comments of the form /* ... */ // Removing comments of the form // // Remove body of the function { ... } // removing '=>' if func is arrow function str = str.replace(/\/\*[\s\S]*?\*\//g, '') .replace(/\/\/(.)*/g, '') .replace(/{[\s\S]*}/, '') .replace(/=>/g, '') .trim(); // Start parameter names after first '(' var start = str.indexOf("(") + 1; // End parameter names is just before last ')' var end = str.length - 1; var result = str.substring(start, end).split(", "); var params = []; result.forEach(element => { // Removing any default value element = element.replace(/=[\s\S]*/g, '').trim(); if(element.length > 0) params.push(element); }); return params; } // Test sample functions var fun1 = function(a){ }; function fun2(a = 5*6/3, // Comment b){ }; var fun3 = (a, /* */ b, //commment c) => /** */{ }; console.log(`List of parameters of ${fun1.name}:`, getParams(fun1)); console.log(`List of parameters of ${fun2.name}:`, getParams(fun2)); console.log(`List of parameters of ${fun3.name}:`, getParams(fun3)); </script> |
chevron_right
filter_none
Output:
List of parameters: List of parameters of fun1: [ 'a' ] List of parameters of fun2: [ 'a', 'b' ] List of parameters of fun3: [ 'a', 'b', 'c' ]
Recommended Posts:
- Passing a function as a parameter in JavaScript
- How to set a default parameter value for a JavaScript function ?
- JavaScript | Pass string parameter in onClick function
- How to add a parameter to the URL in JavaScript ?
- How to dynamically change the title of web page using JavaScript ?
- How to create an image element dynamically using JavaScript ?
- How to merge properties of two JavaScript objects dynamically?
- JavaScript | Rest parameter
- How to change style attribute of an element dynamically using JavaScript ?
- How to dynamically load JS inside JS ?
- Dynamically generating a QR code using PHP
- How to dynamically get the content width of a div using AngularJS ?
- How to create button dynamically with click event in Angular ?
- Argument vs Parameter in Java
- HTML | DOM Parameter Object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



