prettyversatile
prettyversatile

Reputation: 83

When to use Switch and If else statement?

(Sorry for my bad English)

Hi everyone!

I'm new to JS!

I'm learning about Switch currently and a little bit confused about it.

More specifically, when should I use "true" in the switch and when to use the original variable name!

For example, this is my code!

var age = 10;

    switch(true){
            
        case age>=10:
        console.log('He is a kid');
        break;
            
        case age>10 && age<=20:
        console.log('He is a teenager');
        break;
            
        case age>=20 && age<=30:
        console.log('He is a young man');
        break;
            
    
        case age>30:
        console.log('He is a man')
    
        

            
            
    } 

This works totally fine if I do switch(true) but if I do what most people say switch(age), this doesnt work!

So i'm a bit confused when to use what!

My guessing, if I'm doing greater than, equal to and less than type of things with numbers, then you you true in the swithc!

Can anyone please help?

Upvotes: 2

Views: 854

Answers (3)

Mohsen Alyafei
Mohsen Alyafei

Reputation: 5567

You are correct.

The switch matches its "expression" to the case "expression" clause using the strict comparison === and transfers control to that clause.

If your case expressions are to contain other operations (less than, higher, or other complex operation) then you can use "switch (true) {" and do a normal comparison at the "case" level.

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386680

First of all, you had a logical error in the first case expression, you like the check for smaller or equal.

For the next following case expressions, you need only single condition, because you checked before and the conditions breaks the statement.

For the last part, you could take default which is only called if no condition is right.

var age = 10;

switch (true) {
    case age <= 10:
        console.log('He is a kid');
        break;

    case age <= 20:
        console.log('He is a teenager');
        break;

    case age <= 30:
        console.log('He is a young man');
        break;

    default:
      console.log('He is a man')
}

Finally the standard approach (and this is questionalble, too), is to use a function with if statements and exit early, like

function getType(age) {
    if (age <= 10) return 'He is a kid';
    if (age <= 20) return 'He is a teenager';
    if (age <= 30) return 'He is a young man';
    return 'He is a man';
}

Upvotes: 2

vlad katz
vlad katz

Reputation: 522

as stated in MDN: The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.

meaning that the switch statement is for exact matches only. there is no use and no way telling how it will go when using switch(true).

this answer is valid for most programming languages and not only to JS.

Upvotes: 1

Related Questions