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

  • Home
  • SEARCH
  • 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 6781381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:37:02+00:00 2026-05-26T16:37:02+00:00

I’m a bit new to javascript, I’d like my function to take in parameters

  • 0

I’m a bit new to javascript, I’d like my function to take in parameters and associate
them by name,

myFunction(width = 50, height = 20);
myFunction(height = 20, width = 20)
myFunction(width = 50); // height will get some default value

so then I can have:

  function myFunction()
  {
     var width; // this will somehow get the variable that was passed in or set default value
     var height; // this will somehow get the variable that was passed in or set default value
  }

Is there a way to possibly do this in javascript?

Also, I’ve seen syntax of the form

ball: {     
         x: '124',
         y: '22',
      }

but I have no idea what it’s called.

Maybe I could use this syntax somehow, and create some kind of object that I then pass into my function.

But what I’m looking for is:

  1. Pass parameters into a function by name, not by order.
  2. Make all parameters passed into the function optional; if they are not passed in assign a default value.

What’s the best/cleanest way to do this?

  • 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-05-26T16:37:03+00:00Added an answer on May 26, 2026 at 4:37 pm

    Passing an object as single parameter is the way to go.

    Example:

    function func(options) {
        var width = options.width || 0;
        var height = options.height || 0;
        //...
    }
    
    func({height: 100}); // width will be 0 as options.width is undefined
    

    The || shorthand (boolean OR) will set each value to 0 if it is not provided. That is, it will assign 0 if options.width evaluates to false which is the case when it is not defined (i.e. options.width is undefined).

    Whether this is sufficient depends on what kind of values are allowed. E.g. if false is a valid value, you have to explicitly test for undefined.

    Another and arguably more handy way to define default values would be to use an extend function:

    function extend(a, b) {
        for(var prop in b) {
            if(b.hasOwnProperty(prop) && !a.hasOwnProperty(prop)) {
                a[prop] = b[prop];
            }
        }
    }
    

    This copies all properties of object b to object a if they are not present.

    It lets you set default values this way

    function func(options) {
         extend(options, {width: 0, height: 0});
         // access the values with options.height and options.width
         // options.height is not overwritten
    }
    

    regardless of the value.


    This is the way jQuery uses. It provides an $.extend [docs] function which is commonly used when developing plugins to provide default settings. A good example of a function which accepts named and optional parameters is $.ajax [docs].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.