Skip to content
Fix Code Error

Switch statement multiple cases in JavaScript

March 13, 2021 by Code Error
Posted By: Anonymous

I need multiple cases in switch statement in JavaScript, Something like:

switch (varName)
{
   case "afshin", "saeed", "larry":
       alert('Hey');
       break;

   default:
       alert('Default case');
       break;
}

How can I do that? If there’s no way to do something like that in JavaScript, I want to know an alternative solution that also follows the DRY concept.

Solution

Use the fall-through feature of the switch statement. A matched case will run until a break (or the end of the switch statement) is found, so you could write it like:

switch (varName)
{
   case "afshin":
   case "saeed":
   case "larry": 
       alert('Hey');
       break;

   default: 
       alert('Default case');
}
Answered By: Anonymous

Related Articles

  • Use dynamic variable names in `dplyr`
  • How to properly do JSON API GET requests and assign…
  • How do you match strings in custom Instruments…
  • How to refer to a variable inside a function with…
  • Ember link-to throws error / does not clear screen…
  • PHP Pass variable to next page
  • Trying to match today's date with list of custom…
  • Ubuntu apt-get unable to fetch packages
  • Creating a custom counter in Spark based on…
  • Pandas numpy handling Nan
  • AppCompat v7 r21 returning error in values.xml?
  • How to return summed array from computed using…
  • How to find index of an object by key and value in…
  • Simplest way to create Unix-like continuous pipeline…
  • How to use Regular Expressions (Regex) in Microsoft…
  • Smart way to truncate long strings
  • Rebasing remote branches in Git
  • What is the use of a private static variable in Java?
  • Switch statement fall-through...should it be allowed?
  • Flex: REJECT rejects one character at a time?
  • Nextjs using process.env with dynamic variable name
  • The redundancy of forward_iterator concept?
  • Git push rejected after feature branch rebase
  • How can I write a function to detect whether a…
  • Bash - Increment a variable until it can be found in…
  • Copy a file in a sane, safe and efficient way
  • What are the undocumented features and limitations…
  • How can I find the product GUID of an installed MSI setup?
  • What is Ember RunLoop and how does it work?
  • Is eval function necessary here?
  • Multiple separate IF conditions in SQL Server
  • Vue.js - emit to update array not working
  • C++ template,typename and operator
  • Using two values for one switch case statement
  • Getting the name of a variable as a string
  • How does PHP 'foreach' actually work?
  • SQL query return data from multiple tables
  • Backbone.js: TypeError: Object # has no method 'parse'
  • Launching Spring application Address already in use
  • How to unescape a Java string literal in Java?
  • Sending data to TCPServer more than one time
  • Geolocation denied for HTML embedded site - anchor…
  • Elongating a Data Frame in Pandas
  • Can a self-contained C++ concept match a particular…
  • How much is too much when being DRY in my…
  • How to concatenate cell values until it finds a…
  • Switch statement for greater-than/less-than
  • The 'compilation' argument must be an instance of…
  • Named regular expression group "(?Pregexp)": what…
  • Generate an array of years with duplicates of each…
  • Java File - Open A File And Write To It
  • Why does C++ code for testing the Collatz conjecture…
  • SameSite warning Chrome 77
  • data.table vs dplyr: can one do something well the…
  • Beautiful way to remove GET-variables with PHP?
  • easiest way to extract Oracle form xml format data
  • Can't install via pip because of egg_info error
  • How to detect if multiple keys are pressed at once…
  • Start redis-server with config file
  • Error message "Forbidden You don't have permission…
  • Sort list of strings considering two different criteria
  • JavaScript TypeError: Cannot read property 'style' of null
  • Control cannot fall through from one case label
  • Rails rspec returns no examples found when…
  • Get the contents of a table row with a button click
  • How to parse JSON with XE2 dbxJSON
  • Aurelia Features globalResources (jspm)
  • What is your most productive shortcut with Vim?
  • Getting started with Haskell
  • Git workflow and rebase vs merge questions
  • Ukkonen's suffix tree algorithm in plain English
  • How to create an alert the fade after a duration in Vuetify?
  • Switch statement: must default be the last case?
  • Switch case on type c#
  • What are the new features in C++17?
  • Replace multiple strings with multiple other strings
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How can i jump to another def from a def
  • Java - remove last known item from ArrayList
  • Datepicker plugin not working in phonegap 3.0.0
  • JavaScript unit test tools for TDD
  • "Thinking in AngularJS" if I have a jQuery background?
  • Bulk insert into PostgreSQL using dapper in .NET core
  • Handling file renames in git
  • TypeError: Cannot read property 'webpackJsonp' of undefined
  • Write Datatable to a Excel file
  • Python is not calling fucntions properly
  • What does "Fatal error: Unexpectedly found nil while…
  • What does AngularJS do better than jQuery?
  • Wait until all promises complete even if some rejected
  • Error in C++ program (*** Error in `./a.out':…
  • VB.NET Switch Statement GoTo Case
  • Is there a way to have a switch statement for an…
  • Mutex example / tutorial?
  • Firebase cloud function onUpdate is triggered but…
  • Can I run javascript before the whole page is loaded?
  • Oracle: If Table Exists
  • android studio 0.4.2: Gradle project sync failed error
  • Converting Java objects to JSON with Jackson
  • Searching for matching elements between 2 arrays in C

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

What does %s mean in a python format string?

Next Post:

How to use glob() to find files recursively?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error