Javascript三元运算符结果始终相同
下面的代码总是以“hi”作为响应。即使我将活动1变量设置为0。如果我写错了,有人能告诉我吗Javascript三元运算符结果始终相同,javascript,if-statement,operator-keyword,ternary,Javascript,If Statement,Operator Keyword,Ternary,下面的代码总是以“hi”作为响应。即使我将活动1变量设置为0。如果我写错了,有人能告诉我吗 1=2; 活动字符串=(活动类型1>1?“你好”:“你好”)您不需要此“typeof”,因此将其更改为: campaign1 = 2; campaign_string = campaign1 > 1 ? "hello" : "hi" ); 您不需要typeof运算符。试着不那样重复;也要摆脱不必要的妄想 这应该可以 campaign1 = 2; campaign_string = campa
1=2;
活动字符串=(活动类型1>1?“你好”:“你好”)代码>您不需要此“typeof”,因此将其更改为:
campaign1 = 2;
campaign_string = campaign1 > 1 ? "hello" : "hi" );
您不需要typeof
运算符。试着不那样重复;也要摆脱不必要的妄想
这应该可以
campaign1 = 2;
campaign_string = campaign1 > 1 ? "hello" : "hi";
您正在使用typeof
,但未将其与类型进行比较。我想你是有意的
campaign1 = 2;
campaign_string = (campaign1 > 1 ? "hello" : "hi" );
typeof
是这样使用的
var variable = 'Test';
typeof variable === 'string' // This is true.
为什么要使用typeof
来获取变量的值?无论您给出哪个数值,那么typeof activity 1
始终是“number”
。在稍后的代码中,我将字符串与变量activity\u字符串连接起来。有更好的方法吗?因为我不能在字符串concat中使用if语句。