[英]Javascript ternary operator result always the same
下面的代碼總是以“hi”響應。 即使我將campaign1變量設置為0。有人能告訴我我是否寫錯了嗎?
campaign1 = 2; campaign_string = (typeof 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”,所以把它改成:
campaign1 = 2;
campaign_string = campaign1 > 1 ? "hello" : "hi" );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.