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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:56:51+00:00 2026-06-02T12:56:51+00:00

What is the difference between these two ways of checking the arguments? function foo(a,

  • 0

What is the difference between these two ways of checking the arguments?

function foo(a, b) {
    this.a=a;
    this.b=b;
}

Using:

if (arguments.length === 1) {}

or

if (this.b !== undefined) {}
  • 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-02T12:56:52+00:00Added an answer on June 2, 2026 at 12:56 pm

    If you want to check mandatory arguments, I will use arguments.length as first step. If you want to check optional arguments, usually the pattern is:

    function foo(a,b){
        this.a = a || "foo";
        // etc
    }
    

    Notice that this works only if the parameters can’t be “falsy” values (so, empty string, zero, null, undefined, false, NaN). For example, if you pass an empty string as a parameter, you will have foo. If you want to consider only undefined as value for considering a parameter optional then you have to do something like:

    function foo(a){
        this.a = a === undefined ? "foo" : a;
        // etc
    }
    

    If you want to consider both null and undefined and optional parameter, you can have:

    function foo(a){
        this.a = a == undefined ? "foo" : a;
        // etc
    }
    

    Of course, you can also use the operator typeof. For instance you want that a can be only a string:

    function foo(a) {
        this.a = typeof a === "string" ? a : "";
        // etc
    }
    

    You can also force a to be always a string, in the worst scenario will be the string version of the non-string value given (undefined as well):

    function foo(a) {
        this.a = String(a);
        // etc
    }
    

    More complex cases have some utility functions that makes all this check for you, where you said the arguments required, the optional, the default value, etc.

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

Sidebar

Related Questions

What's the difference between these two ways of declaring a function? function someFunc() {
Is there a difference between these two ways of default checking a checkbox: document.getElementById(checkboxId).defaultChecked
Can anybody tell me the difference between these two ways of formatting numbers using
In Javascript is there any difference between these two ways of adding a function
What is the difference between these two ways of saving data in CakePHP $this->request->data['User']['status']
Is there a difference (performance, overhead) between these two ways of merging data sets?
Will there be a difference between the following two ways of calling a function
What is the difference between these two: font-style:italic font-style:oblique I tried using the W3Schools
What is the difference between these two functions? 1: $(document).ready(function myfunc() { function dosomething()
What is the main difference between these following two ways to give a method

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.