How to get the javascript function parameter names/values dynamically ? Last Updated : 17 Jul, 2023 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 infoAdvertise with us Next Article How To Include a JavaScript File in Another JavaScript File? V Vinod Tahelyani Follow Improve Article Tags : JavaScript Web Technologies javascript-functions JavaScript-Questions Similar Reads How to write a function in JavaScript ? JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod 4 min read What is Currying Function in JavaScript? Currying is used in JavaScript to break down complex function calls into smaller, more manageable steps. It transforms a function with multiple arguments into a series of functions, each taking a single argument.It converts a function with multiple parameters into a sequence of functions.Each functi 3 min read How to call JavaScript function in HTML ? In HTML, you can easily call JavaScript functions using event attributes like onclick and onload. Just reference the function name within these attributes to trigger it. You can also call functions directly within script blocks using standard JavaScript syntax. Let's create an HTML structure with so 2 min read setTimeout() in JavaScript The setTimeout() function is used to add delay or scheduling the execution of a specific function after a certain period. It's a key feature of both browser environments and Node.js, enabling asynchronous behavior in code execution.JavaScriptsetTimeout(function() { console.log('Hello, world!'); }, 2 2 min read What is a typical use case for anonymous functions in JavaScript ? In this article, we will try to understand what exactly an Anonymous function is, and how we could declare it using the syntax provided in JavaScript further we will see some examples (use-cases) where we can use anonymous functions to get our results in the console. Before proceeding with the examp 4 min read How to check whether a number is NaN or finite in JavaScript ? When working with numbers in JavaScript, it's important to know how to determine if a value is NaN (Not-a-Number) or finite. This knowledge is crucial for data validation, error handling, and ensuring your code behaves as expected. In this article, we will see how to check whether the number is NaN 3 min read How to Encode and Decode a URL in JavaScript? Encoding and decoding URLs in JavaScript is essential for web development, especially when making GET requests with query parameters. This process ensures special characters in URLs are correctly interpreted by the server. For instance, spaces are converted to %20 or + in URLs. This guide covers how 4 min read How to declare the optional function parameters in JavaScript ? Declaring optional function parameters in JavaScript means defining function parameters that aren't required when the function is called. You can assign default values to these parameters using the = syntax, so if no argument is provided, the default value is used instead. These are the following ap 3 min read How to get the javascript function parameter names/values dynamically ? 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 na 2 min read How To Include a JavaScript File in Another JavaScript File? The import and export syntax available in ES6 (ECMAScript 2015) module is used to include a JavaScript file in another JavaScript file. This approach is the most modern and recommended way to share code between JavaScript files.It allows you to break your code into smaller modules and then import th 4 min read Like