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 8101205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:55:38+00:00 2026-06-05T22:55:38+00:00

Question What is a good way of assigning a default value to an optional

  • 0

Question

What is a good way of assigning a default value to an optional parameter?

Background

I’m playing around with optional parameters in JavaScript, and the idea of assigning a default value if a parameter is not specified. My method in question accepts two parameters, the latter of which I deem to be optional, and if unspecified should default to false. My method looks something like this…

// selects the item, appropriately updating any siblings
// item to select [, toggle on / off]
this.selectItem = function(item, toggle)
{
    toggle = toggle && typeof toggle === 'boolean';
    if (toggle)
    {
       // ...
    }
}

Testing

After running a few tests on this jsFiddle, using the following principal for default value assigning:

function checkParamIsBoolean(param)
{
    param = param && typeof param === 'boolean';
}

checkParamIsBoolean('me'); // boolean
checkParamIsBoolean([]); // boolean
checkParamIsBoolean(true); // boolean
checkParamIsBoolean(false); // boolean
checkParamIsBoolean(1 == 1); // boolean
checkParamIsBoolean(1); // boolean
checkParamIsBoolean(null); // object
checkParamIsBoolean(undefined); // undefined
​

As you can, the results vary, and aren’t desired.

Expected

null = false
undefined = false

Actual

null = object
undefined = undefined

Summary

Are there any alternative approaches to assigning a default value to an optional parameter if it’s unspecified; would it be better to use _toggle as the parameter and then assign the value to var toggle within the method?

  • 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-05T22:55:39+00:00Added an answer on June 5, 2026 at 10:55 pm

    Better solution is to use named arguments wraped in a object. It keeps your code clean and prevents errors in case of complex functions (with many default and non-default arguments). Otherwise you need to remember about the order of arguments and assign default values based on the types of arguments present (that’s what you’re trying to do).

    That method is a primary way of passing params jQuery and jQuery’s plugins.

    function test(options) { 
    
      // set up default options 
      var defaults = { 
        param1:      'test', 
        param2:      100
      }; 
    
      // combine options with default values
      var options = $.extend({}, defaults, options); // If you're not using jQuery you need different function here
    
      alert(options.param1)
      alert(options.param2)
    
    }
    
    test({'param2': 200}) // Call the function with just the second param
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel this is not a very good question to post on SO, but
I hope my question made sense. I'm wondering if there is a good way
I had trouble coming up with a good way to word this question, so
Question: Is there a good way in Silverlight to intercept undesirable characters from being
Google app engine question: What is a good way to take formatted text (does
This is a theoretical question, I was wondering if there is a good way
My Question: What is a good way to deal with two different versions of
I believe that the question Is there a good way to do this type
Sorry for the simple question but I can't think of a good way to
My question: What's a good way to parse the information below? I have a

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.