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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:18:05+00:00 2026-06-14T14:18:05+00:00

This has come up a few times in a project I’m working on, how

  • 0

This has come up a few times in a project I’m working on, how can I “test” a switch to determine if it has a case in it without actually executing it?

If the case has to be run, is there an efficient way to check?

Thank you in advance.

i.e.

if (runSwitch(switch.hasCase("casename2"))) {
    alert("I've got it!");
}
else {
    alert("nope");
}

function runSwitch(case) {
    switch (case) { // Any way to skip the function?
        case "casename0" : alert("case"); break;
        case "casename1" : alert("case"); break;
        case "casename2" : alert("case"); break;
        case "casename3" : alert("case"); break;
    }
}
  • 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-14T14:18:06+00:00Added an answer on June 14, 2026 at 2:18 pm

    Since David Schwartz didn’t post an answer, I’m going to post my solution (it’s hardly a solution) and a demo and explanation of his solution as I understand it.

    My solution:

    I simply stopped using switches and switched to JSON (arrays) since the only purpose of my switch was to set variables depending on the input.

    When using an array, checking to see if a “case” exists is easy (just run arrayVar["casename"], it returns undefined if it doesn’t exist) and you don’t need to clog up the namespace with extra variables, running code is slightly more difficult as it needs to be provided as a string and evaled but overall this works much better for me.

    There’s no need to post a demo or code since it really isn’t a solution to this issue.

    David Schwartz’s solution:

    Demo: http://jsfiddle.net/SO_AMK/s9MhD/

    Code:

    function hasCase(caseName) { return switchName(caseName, true); } // A function to check for a case, this passes the case name to switchName, sets "just checking" to true and passes the response along
    function runSwitch(caseName) { switchName(caseName); } // This is actually a mostly useless function but it helps clarify things
    
    function switchName(caseName, c) { // And the actual function, c is the "just checking" variable, I shortened it's name to save code
        switch(caseName) { // The switch
            case "casename0" : if (c) return true; alert("case"); break; // If c is true return true otherwise continue with the case, returning from inside a case is the same as calling break;
            case "casename1" : if (c) return true; alert("case"); break;
            case "casename2" : if (c) return true; alert("case"); break;
            case "casename3" : if (c) return true; alert("case"); break;
            default: if (c) return false; break; // If nothing else ran then the case doesn't exist, return false
        }
        return c; // Even I (:D) don't understand where this gets changed, but it works
    }
    

    Explanation: The code above is commented as needed.
    ​

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

Sidebar

Related Questions

I know this error has come up a few times, but I'm still not
This is a question that has come up a few times on here in
This has been an annoyance I've come across a few times so I thought
From what I've read, this topic has come up several times over the last
I've seen this come up a few times but I haven't really seen a
I know, this has been asked a few times, but mostly for Xcode 3.x.
I know this question has been asked a few times. I understand how to
I have come across this problem a few times and never been able to
I know this has been asked several times, but after a few hours of
A situation has come up a few times in the last few weeks when

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.