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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:44:12+00:00 2026-05-10T23:44:12+00:00

I have now seen 2 methods for determining if an argument has been passed

  • 0

I have now seen 2 methods for determining if an argument has been passed to a JavaScript function. I’m wondering if one method is better than the other or if one is just bad to use?

 function Test(argument1, argument2) {       if (Test.arguments.length == 1) argument2 = 'blah';        alert(argument2);  }   Test('test'); 

Or

 function Test(argument1, argument2) {       argument2 = argument2 || 'blah';        alert(argument2);  }   Test('test'); 

As far as I can tell, they both result in the same thing, but I’ve only used the first one before in production.

Another Option as mentioned by Tom:

function Test(argument1, argument2) {     if(argument2 === null) {         argument2 = 'blah';     }      alert(argument2); } 

As per Juan’s comment, it would be better to change Tom’s suggestion to:

function Test(argument1, argument2) {     if(argument2 === undefined) {         argument2 = 'blah';     }      alert(argument2); } 
  • 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-10T23:44:13+00:00Added an answer on May 10, 2026 at 11:44 pm

    There are several different ways to check if an argument was passed to a function. In addition to the two you mentioned in your (original) question – checking arguments.length or using the || operator to provide default values – one can also explicitly check the arguments for undefined via argument2 === undefined or typeof argument2 === 'undefined' if one is paranoid (see comments).

    Using the || operator has become standard practice – all the cool kids do it – but be careful: The default value will be triggered if the argument evaluates to false, which means it might actually be undefined, null, false, 0, '' (or anything else for which Boolean(...) returns false).

    So the question is when to use which check, as they all yield slightly different results.

    Checking arguments.length exhibits the ‘most correct’ behaviour, but it might not be feasible if there’s more than one optional argument.

    The test for undefined is next ‘best’ – it only ‘fails’ if the function is explicitly called with an undefined value, which in all likelyhood should be treated the same way as omitting the argument.

    The use of the || operator might trigger usage of the default value even if a valid argument is provided. On the other hand, its behaviour might actually be desired.

    To summarize: Only use it if you know what you’re doing!

    In my opinion, using || is also the way to go if there’s more than one optional argument and one doesn’t want to pass an object literal as a workaround for named parameters.

    Another nice way to provide default values using arguments.length is possible by falling through the labels of a switch statement:

    function test(requiredArg, optionalArg1, optionalArg2, optionalArg3) {     switch(arguments.length) {         case 1: optionalArg1 = 'default1';         case 2: optionalArg2 = 'default2';         case 3: optionalArg3 = 'default3';         case 4: break;         default: throw new Error('illegal argument count')     }     // do stuff } 

    This has the downside that the programmer’s intention is not (visually) obvious and uses ‘magic numbers’; it is therefore possibly error prone.

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

Sidebar

Ask A Question

Stats

  • Questions 104k
  • Answers 104k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Custom events are in the W3C spec, but none of… May 11, 2026 at 8:34 pm
  • Editorial Team
    Editorial Team added an answer In my experience this feature is not ubiquitous across all… May 11, 2026 at 8:34 pm
  • Editorial Team
    Editorial Team added an answer Maybe this could help: Add DNS Resource EDIT: Here's a… May 11, 2026 at 8:34 pm

Related Questions

I am confused as to when I can use the DOM properties and when
I've had someone else's pile of stuff handed to me. Among these items is
I have a query in Delphi using DBExpress TSQLQuery that looks like so ActiveSQL.sql.add('SELECT
I'm trying to create a function in C# which will allow me to, when

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.