以下javascript语句如何评估?

时间:2013-08-18 05:01:16

标签: javascript

以下语句没有if语句,即使它使用?字符。它是如何评估的?

var username = req.user ? req.user.username : '';

1 个答案:

答案 0 :(得分:6)

它等同于:

var username;

if (req.user) {
    username = req.user.username;
} else {
    username = '';
}

Read more about ternary operators on MDN