📜  带有数字范围的 switch case - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:15.843000             🧑  作者: Mango

代码示例2
function getCategory(age) {  
    var category = "";  
    switch (true) {  
        case isNaN(age):  
            category = "not an age";  
            break;  
        case (age >= 50):  
            category = "Old";  
            break;  
        case (age <= 20):  
            category = "Baby";  
            break;  
        default:  
            category = "Young";  
            break;  
    };  
    return category;  
}  
getCategory(5);  // will return "Baby"