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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:14:59+00:00 2026-05-28T01:14:59+00:00

I have recently learned that you can use a neat switch statement with fallthrough

  • 0

I have recently learned that you can use a neat switch statement with fallthrough to set default argument values in Javascript:

function myFunc(arg1, arg2, arg3) {
    //replace unpassed arguments with their defaults:
    switch (arguments.length) {
        case 0 : arg1 = "default1";
        case 1 : arg2 = "default2";
        case 2 : arg3 = "default3";
    }
}

I have grown to like it a lot, since not only is it very short but it also works based on parameters actually passed, without relying on having a special class of values (null, falsy, etc) serve as placeholders as in the more traditional versions:

function myFunc(arg1, arg2, arg3){
    //replace falsy arguments with their defaults:
    arg1 = arg1 || "default1";
    arg2 = arg2 || "default2";
    arg3 = arg3 || "default3";
}

My inital though after seeing the version using the switch was that I should consider using it “by default” over the || version.

The switch fallthough makes it not much longer and it has the advantage that it is much more “robust” in that it does not care about the types of the parameters. In the general case, it sounds like a good idea to not have to worry about what would happen with all the falsy values (”, 0, null, false …) whenever I have to make a function with default parameters.

I would then reserve the arg = arg || x for the actual cases where I want to check for truthyness instead of repurposing it as the general rule for parameter defaulting.

However, I found very few examples of this pattern when I did a code search for it so I had to put on my skeptic hat. Why didn’t I find more examples of this idiom?

  • Is it just now very well known?
  • Did I not search well enough? Did I get confused by the large number of false positives?
  • Is there something that makes it inferior to the alternatives?

Some reasons that I (and some of the comments) could think of for avoiding switch(arguments.length):

  • Using named parameters passed via an object literal is very flexible and extensible. Perhaps places where more arguments can be optional are using this instead?

  • Perhaps most of the time we do want to check for truthyness? Using a category of values as palceholders also allows default parameters to appear in the middle instead of only at the end : myFunc('arg1', null, 'arg3')

  • Perhaps most people just prefer the very short arg = arg || "default" and most of the time we just don’t care about falsy values?

  • Perhaps accessing arguements is evil/unperformant?

  • Perhaps this kind of switch case fallthrough has a bad part I didn’t think about?

Are these cons enough to avoid using switch(arguments.length) as a staple default argument pattern or is it a neat trick I should keep and use in my code?

  • 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-05-28T01:14:59+00:00Added an answer on May 28, 2026 at 1:14 am

    Since the question has been updated, it’s really a matter of opinion. There are a number of javascript features that many people suggest avoiding, such as switch and ternary. This is why there is not a lot of information on some of those features.

    The reason that suggestion is made is because many people miss-use those features and create problems in their code. The bugs are sometimes difficult to detect and it can be difficult for others to understand what your code is doing (particularly those unfamiliar with javascript or new programmers).

    So if you like to do it that way, and you’re not worried about the opinions (or skill level) of anyone working on your code. By all means, your approach will work. I have used the switch statement myself on occasion, and while I don’t think it’s really “good” or “bad”, it’s hard to find a situation that requires it.

    You asked how I might go about this without an if-else chain:

    function myFunc(args) {
        var allArgs = {
            arg1:"default1",
            arg2:"default2",
            arg3:"default3"
        };
        for (var key in args) {
            allArgs[key] = args[key];        
        }
    }
    myFunc({arg1:null, arg3:'test'})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently learned that python doesn't have the switch/case statement. I've been reading about
I have learned recently that Export in Mathematica uses by default the Printout screen
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I have recently learned that sql server 2005 does not support UTF8: UTF8 problem
Possible Duplicate: Using A* to solve Travelling Salesman Problem I have recently learned that
I'm fairly new to C#, and have recently learned that it's possible to emit
From an old answer I have recently learned that Google Analytics, besides the traditional
I have recently learned that there are bugs in JDK7 for hotspot compiler optimization.
I have recently learned that if you have a reference to a class as
I am early on in a REST implementation and have recently learned that we

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.