How can I pass a function parameter as a switch statement case value?

javascript switch case multiple expressions
switch statement javascript
onclick switch statement javascript
javascript switch multiple variables
javascript switch return
javascript switch case range
switch case array javascript
js switch statement with strings

I am testing out switch statements in JavaScript and wanting to write one into a function for repeated execution.

I have been writing a basic function that takes either "male" or "female" as parameters, and logs one or the other to the console: "its a boy!" or "its a girl!", respectively.

function checkGender(gender){
    gender = "";
    switch (gender){
    case "male":
     console.log("it's a boy!");
     // have also tried using a return statement.
     break;
    case "female":
     console.log("it's a girl!");
     break;
    }
   }

    checkGender("male");
    // => should return "it's a boy!".

Expected: Invoking the function should return log statement to the console.

Actual Results: The console returns "undefined" as a value.


You are overriding the gender value supplied to function by setting gender='' inside the function block. Based on the new gender value (which is empty), there is no case present and hence nothing in the console. Do not reset the gender value supplied to your function and it will work:

function checkGender(gender) {
  switch (gender) {
    case "male":
      console.log("it's a boy!");
      // have also tried using a return statement.
      break;
    case "female":
      console.log("it's a girl!");
      break;
  }
}

checkGender("male");

Conditional Statements: If, Else, and Switch, How do you pass multiple values in a switch case? In this program, since the value stored in variable num is eight, a switch will execute the case whose case-label is 8. After executing the case, the control will fall out of the switch and program will be terminated with the successful result by printing the value on the output screen.


You are setting the variable gender to nothing "", therefore the case function will not see male or female, and will thus return nothing, or undefined.

Can I use a case/switch statement with two variables?, How do you call a function inside a switch case? When you try to pass on a value to another underlying function, – SwitchParameterInOtherFunction $switchParameterInThis function will set the switch to true in the underlying function, and then pass a positional parameter with the value of the switch parameter. The V1 fix for this is to put a : between the parameter name and its value.


The first instruction of your function affects an empty string to the gender parameter. Thus, none of the cases matches.

By the way, the checkGender function does not return anything. It only performs a console.log (if one case of the switch matches). Its return value will always be undefined.

The switch Statement (The Java™ Tutorials > Learning the Java , : "int fc = ", just use "fc = " How to consume switch parameter? Demo function if switch -colortext is included you get color text output if not plain text with default host color. Function Demo


Calling a function within a switch case, Which clearaly states tha multiple parameters do work. We can also reverse the script by using the multiple parameters in the switch instead of in the case: function  Can a switch statement have multiple parameters? We can also reverse the script by using the multiple parameters in the switch instead of in the case: function


Can a switch statement have multiple parameters?, The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, This method takes advantage of the fact that if there is no break below a case Arrow function expressions · Default parameters · Method definitions · Rest  The following example shows a simple switch statement that has three switch sections, each containing two statements. The second switch section contains the case 2: and case 3: labels. A switch statement can include any number of switch sections, and each section can have one or more case labels, as shown in the following example. However, no


switch, The comparison value will match either a statement value or trigger a default switch (text) // Passing the variable to switch condition{ case "Hello 1": the code to execute into a separate method with it's own logic evaluation. Using PowerShell Switch vs. Boolean Parameters in SMA Runbooks. Hey Readers. In this article, I wanted to take the opportunity to talk about a scenario that may be “edge case” but a case just the same within the constructs of leveraging switch parameters in PowerShell workflow.