如果/否则或者只是如果?

2025-06-10

如果/否则或者只是如果?

今天在代码审查中,我们收到了这样的声明:

声明A

if (typeof val === 'string') {
    return 'A'
} else if (val === null || val === undefined) {
    return 'B'
} else {
    return val
}
Enter fullscreen mode Exit fullscreen mode

并提出了改写如下的建议:

声明 B

if (typeof val === 'string') return 'A'
if (val === null || val === undefined) return 'B'
return val
Enter fullscreen mode Exit fullscreen mode

我不会告诉你我们从哪里出来了😜,但你觉得哪种方式更好?

简洁、避免逻辑的建议是if/else好的,还是原来的方式更好?

鏂囩珷鏉ユ簮锛�https://dev.to/tyrw/ifelse-or-just-if-1kie
PREV
模块星期一:轮播幻灯片、粘性页脚、电话覆盖等
NEXT
新的 Web 代码编辑器