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 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
  • 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. 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 wish to know all the pros and cons about using these two methods.
Currently I know of only two ways to cache data (I use PHP but
I would like to update my SQL lite database with the native update-method of
I know two approaches to Exception handling, lets have a look at them. Contract
To start, I know there are two kinds of JSON serialization currently built into
do you know any good algorithms that match two strings and then return a
Do you know of any compilers that only requires one or two clicks on
Does anyone know of a simple way to compare two strings together to generate
I have two keyboards connected to my PC, is there any way to know
I have two computers. Both running WinXP SP2 (I don't really know ho similar

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.