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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:44:25+00:00 2026-06-08T12:44:25+00:00

Since NaN === NaN evaluates to false , is it possible to add a

  • 0

Since NaN === NaN evaluates to false, is it possible to add a NaN case to a switch statement?

For example, let’s say I want to make the following switch:

switch(x){
    case 1:
    case 2:
    case 4:
        doSomething();
        break;
    case NaN:
        doSomethingElse();
        break;
    case default:
        doADifferentThing();
        break;
}

Sending NaN as x will go to the default case. I know there are ways around using NaN in switch statements (e.g. I can surround with an if..else statement and use isNaN), but is there a more direct approach?

  • 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-08T12:44:27+00:00Added an answer on June 8, 2026 at 12:44 pm

    I originally wrote i saw only one solution, however during my sleep i came up with a superb solution.

    Always keep in mind that a switch does not do implicit type conversion to compare the cases so if you provide a string to the switch statement it will not match to integers in the cases, and vice versa.
    If you want to compare to strings and integers you will have to cast your integer to a string first and then compare to strings only.

    The superb solution:

    As pointed out by WouterH, my initial solution will resolve to default when using a string that contains a number, this is expected behavior for switch statements. But it might be useful to parse the argument in order to overcome this. For that you can use following code:

    var x = "1";
    switch (isNaN(x) || parseInt(x))
    {
        case true:
          alert("IsNaN!") 
          break; 
        case 1: 
          alert("1"); 
          break; 
        case 2: 
          alert("2"); 
          break; 
        case 4: 
          alert("4"); 
          break; 
        default: 
          alert("default"); 
          break; 
    }
    

    My initial superb method :

    var x = "clearly not a number";
    switch(x){
        case !isNaN(x) || x:
          alert("IsNaN!")
          break;
        case 1:
          alert("1");
          break;
        case 2:
          alert("2");
          break;
        case 4:
          alert("4");
          break;
        default:
          alert("default");
          break;
        }
    

    isNaN will return true if x where to be a string but it doesn’t really matter because true won’t evaluate as true to a string because of the above mentioned behavior of the switch statement.

    My original solution:

    I don’t even know what i was thinking, this looks horrible and the indentation is just plain awkward, but thanks for the upvotes !

    var x = "clearly not a number";
    switch(x){
        case 1:
          alert("1");
          break;
        case 2:
          alert("2");
          break;
        case 4:
          alert("4");
          break;
        case default:
           if (isNaN(x)){
              alert("isNaN");
              break;
           }
           alert("default");
           break;
    }
    

    Brad’s solution:

    thx to Brad for this one.
    I don’t really like this because it feels a bit like a hack, that is to say, this isn’t how you would expect usage of a case statement, but it does give you the most flexibility, so i’m certain there is a use case for it.

    var x = "clearly not a number";
    switch(true)
    {
       case x==1:
          alert("1");
          break;
       case x==2:
          alert("2");
          break;
       case IsNaN(x):
          alert("IsNaN");
          break;
       case default:
          alert("default");
          break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

since memcpy should be highly optimized nowadays, does it still make sense to optimize
Since input and raw_input() stop the program from running anymore, I want to use
With NaN, it is possible to get a list that will not properly sort:
Since hist() of the base R does not report percentages (and the freq=FALSE) does
It is often hard to find the origin of a NaN, since it can
I'm having a trouble since jQuery.ajax, in this particular case, doesn't seem to happen,
since undefined equates to false, i am wondering if there are any draw backs
The following script works on all browsers and devices, but since the release of
Since I develop on localhost but deploy elsewhere, and since I don't want to
Since regular jQuery animations are not fluent on iOS ( .hide() , slideDown() ),

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.