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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:46:44+00:00 2026-05-11T02:46:44+00:00

I know of two methods of setting a default parameter, but I would like

  • 0

I know of two methods of setting a default parameter, but I would like to know what the preferred method is.

function Foo(par1, par2) {     if(par2 == null)         par2 = 'my default' } 

or

function Foo(par1, par2) {     par2 = par2 || 'my default' } 

or is there a better way than either of these?

EDIT:

I would also like to know how others handle multiple optional parameters such as this: We have several functions like this in internal libraries (I think they’re pretty ugly).

function Foo(par1, par2, par3) {     if(par2 == null)         par2 = 'my default'     if(par3 == null)         par3 = 'my default'     // Do something } 

And to call it:

Foo('Parameter one',null,true) 
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T02:46:45+00:00Added an answer on May 11, 2026 at 2:46 am

    Personally I think the second form is just outright cleaner and more readable, certainly moreso than sergey’s arguments inspection – though that can have it’s place – but I prefer to pass extensible objects for args than have to maintain arg signatures.

    e.g.

    function Foo(myArgs) {     myArgs.par1 = myArgs.par1 || 'my default'     myArgs.par2 = myArgs.par2 || 'my default' } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two methods in my NSObject. I would like to know how to
I know two methods of copying a postgres database, but both of them require
I know of two methods which are not reliable: _access() - doesn't work on
I know two ways to check parameters of the method and throw exceptions when
I know two of them are IERegCreateKeyEx() and IERegSetValueEx(). But I don't know what
I have two webservices that have exactly the same methods and signatures but point
I know two 3D-points in a line (top and bottom of an unsymmetrical object),
I know two arrays can be zipped and the result can be iterated with
Let's say I know two points that make up a line. I want to
As we know two Set instances are equal iff they contain the same elements

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.