How do you have a NaN case in a switch statement?

后端 未结 4 1451
滥情空心
滥情空心 2021-02-04 12:20

Since NaN === NaN evaluates to false, is it possible to add a NaN case to a switch statement?

For example, let\'s say

4条回答
  •  北海茫月
    2021-02-04 12:23

    use toString():

    switch (x.toString()) {
        case '1':
        case '2':
        case '4':
            console.log('1/2/4');
            break;
        case 'NaN':
            console.log('NaN');
            break;
        default:
            console.log('default');
    }
    

提交回复
热议问题