1

Is it possible to execute a case block based on a function call/expression evaluation?

function decideRandomName(name) {
let n
    switch (name) {
        case name.toString().toUpperCase().startsWith("A"): // Is this possible?
            console.log("here")
            n = "Andshand"
            break;
        case name.toString().toUpperCase().startsWith("B"):
            n = "Bandshand"
            break;
        default:
            n = "Pandu saala"
    }
    return n;
}

When i execute this, it always executes the default block. If this kind of syntax is not supported then i believe js to throw me an error? Can someone explain the concept here?

3 Answers 3

3

Just switch for truthyness:

 switch (true) { }

However in your case you could do:

 return {
  "J": "Jonas",
  "A": "Albert"
 }[name[0].toUpperCase()] || "default";
Sign up to request clarification or add additional context in comments.

7 Comments

Can you also explain the return object syntax you used? And if i am not wrong that return statement is a replacement for switch?
@eNthUsiAst yes it is.
Can you also please explain this syntax? or provide a link to documentation usage of this syntax?
Thats just an object and bracket access notation.
But how does that work as a switch? It should simply return the entire object?
|
1

Switch statements are used to evaluate different possible values of an expression. They can be used where you might otherwise have a control flow structure like this:

if(someLetter === "A") {
    // do whatever for "A"
} else if (someLetter === "B") {
    // do whatever for "B"
} else if (someLetter === "C") {
    // etc.,
    // etc., and like so for D through Z
    // etc., 
} else {
    // default action
}

In your code, name is (presumably) a string. But, according to MDN, .startsWith() returns true or false. So your cases are basically equivalent to case false: and case false:. And, since the value of name is not false, you're not hitting those cases.

I updated your code to demonstrate the intended usage of switch.

function decideRandomName(name) {
    let n;
    let firstLetter = name[0].toUpperCase();
    switch (firstLetter) {
        case "A": 
            console.log("here")
            n = "Andshand"
            break;
        case "B":
            n = "Bandshand"
            break;
        default:
            n = "Pandu saala"
            break;
    }
    return n;
}

Comments

0

The matching that occurs between the expression passed to switch, (name in this case) and each case expression is identical to the === algorithm.

Since name (a string presumably) and name.toString().toUpperCase().startsWith("A") (a boolean value) can never be identical, none of the cases match and the default block is executed.

As a work around you could do as @Jonas suggested and check for truthyness by:

 switch (true) { }

2 Comments

Thanks, understood it now :)
@eNthUsiAst glad to have helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.