首页 > 解决方案 > 如何在条件中使用 javascript 函数参数(字符串)

问题描述

这里是 js 的新手......出于必要使用一点:-)。

当参数(x)将“notionalLarge”写入控制台时,为什么我有条件地向控制台写入“shares”?

    console.log(x);
    if(x == 'sharesLarge' || 'sharesSmall' ) {
        console.log("shares");
    } else if (x == 'notionalLarge' || 'notionalSmall' ) {
        console.log("notional");
    } else {
        console.log("error");
    }
}```

CONSOLE:
[Log] notionalLarge (sharesOrNotional.js, line 2)
[Log] shares (sharesOrNotional.js, line 4)

标签: javascript

解决方案


你的 if 条件是错误的。

if 条件将分为两部分:

X == 'sharesLarge'

这是错误的

'小股'

这等于

'SharesSmall' != null

这是真的。所以你的第一个 if 条件为真。

你想要达到的是

If(x == 'sharesLarge' || x == 'sharesSmall')

推荐阅读