How to get the javascript function parameter names/values dynamically ? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we are given any arbitrary JavaScript function and the task is to return the parameter names of the function. Approach: JavaScript contains a method called toString() which is used to represent a function code in 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 removes all comments, function body, white spaces, and ES6 arrow.Also, the last character will be just after the end of the parameters.Example: This example explains the above-explained approach. JavaScript // JavaScript program to get the function // name/values dynamically function getParams(func) { // String representation of the function code let 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 '(' let start = str.indexOf("(") + 1; // End parameter names is just before last ')' let end = str.length - 1; let result = str.substring(start, end).split(", "); let 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 let fun1 = function (a) { }; function fun2(a = 5 * 6 / 3, b) { }; let fun3 = (a, /* */ b, //comment 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)); OutputList of parameters of fun1: [ 'a' ] List of parameters of fun2: [ 'a', 'b' ] List of parameters of fun3: [ 'a', 'b', 'c' ] Comment More info V Vinod Tahelyani Follow Improve Article Tags : JavaScript Web Technologies javascript-functions JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like