通常我用这个:
myVar = "myString is" + 1 === 1 ? " really true" : " false, I think";
也许我只需要真实的部分,比方说:
myVar = "myString is" + 1 === 1 ? " really true" : "";
我不喜欢这部分:: ""
因为没用。
有没有办法使用类似下面的东西?
myVar = "myString is" + 1 === 1 && " really true";
它可以工作,但是当 is 为 false 时会出现问题,因为它会写入 "false"
!
最佳答案
你总是可以使用一个好的旧的 if 语句
var myVar = 'myString is';
if (1===1){myVar+=' really true';}
我认为这比单行 bool 测试更具可读性
关于Javascript,没有三元运算符的字符串连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51640005/