Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8343707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:08:53+00:00 2026-06-09T06:08:53+00:00

C#, for example, allows using named parameters like so: calculateBMI(70, height: 175); I find

  • 0

C#, for example, allows using named parameters like so:

calculateBMI(70, height: 175);

I find this quite useful. How can I get a similar effect in JavaScript?

I’ve tried doing things like

myFunction({ param1: 70, param2: 175 });

function myFunction(params){
  // Check if params is an object
  // Check if the parameters I need are non-null
  // Blah blah
}

but it seems awkward. Is there a simpler way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:08:56+00:00Added an answer on June 9, 2026 at 6:08 am

    ES2015 and later

    In ES2015, parameter destructuring can be used to simulate named parameters. It would require the caller to pass an object, but you can avoid all of the checks inside the function if you also use default parameters:

    myFunction({ param1 : 70, param2 : 175});
    
    function myFunction({param1, param2}={}){
      // ...function body...
    }
    
    // Or with defaults, 
    function myFunc({
      name = 'Default user',
      age = 'N/A'
    }={}) {
      // ...function body...
    }
    

    ES5

    There is a way to come close to what you want, but it is based on the output of Function.prototype.toString [ES5], which is implementation dependent to some degree, so it might not be cross-browser compatible.

    The idea is to parse the parameter names from the string representation of the function so that you can associate the properties of an object with the corresponding parameter.

    A function call could then look like

    func(a, b, {someArg: ..., someOtherArg: ...});
    

    where a and b are positional arguments and the last argument is an object with named arguments.

    For example:

    var parameterfy = (function() {
        var pattern = /function[^(]*\(([^)]*)\)/;
    
        return function(func) {
            // fails horribly for parameterless functions ;)
            var args = func.toString().match(pattern)[1].split(/,\s*/);
    
            return function() {
                var named_params = arguments[arguments.length - 1];
                if (typeof named_params === 'object') {
                    var params = [].slice.call(arguments, 0, -1);
                    if (params.length < args.length) {
                        for (var i = params.length, l = args.length; i < l; i++) {
                            params.push(named_params[args[i]]);
                        }
                        return func.apply(this, params);
                    }
                }
                return func.apply(null, arguments);
            };
        };
    }());
    

    Which you would use as:

    var foo = parameterfy(function(a, b, c) {
        console.log('a is ' + a, ' | b is ' + b, ' | c is ' + c);     
    });
    
    foo(1, 2, 3); // a is 1  | b is 2  | c is 3
    foo(1, {b:2, c:3}); // a is 1  | b is 2  | c is 3
    foo(1, {c:3}); // a is 1  | b is undefined  | c is 3
    foo({a: 1, c:3}); // a is 1  | b is undefined  | c is 3 
    

    DEMO

    There are some drawbacks to this approach (you have been warned!):

    • If the last argument is an object, it is treated as a "named argument objects"
    • You will always get as many arguments as you defined in the function, but some of them might have the value undefined (that’s different from having no value at all). That means you cannot use arguments.length to test how many arguments have been passed.

    Instead of having a function creating the wrapper, you could also have a function which accepts a function and various values as arguments, such as

    call(func, a, b, {posArg: ... });
    

    or even extend Function.prototype so that you could do:

    foo.execute(a, b, {posArg: ...});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

CSS3 allows developers to implement simple graphics without using images. For example this envelope
Currently my code looks like this. It allows me to parse multiple parameters my
So, I took some code from this Microsoft provided Example which allows me to
Using the shell, I can do this: >>> from django.test.client import Client >>> from
I am using the jQuery Tools library Tabs. You can find them here: http://flowplayer.org/tools/tabs/index.html
I have one file named traffic that looks like this: city statenum casenum vnumber
MongoDB's PHP library allows me to connect to a collection like this ( from
Example: http://jqueryui.com/demos/dialog/#modal-confirmation The example shown allows you to view the source and even has
Example: http://vincent-massaro.com/map/ Currently, the script allows you to click on a piece of the
I have a script which allows to display favicons based on the url: Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.