Javascript 如何在if语句中不返回任何内容而不返回空白?
您可以看到,我使用“”使if语句在某些情况下不返回任何内容,但实际上它返回一个空格。我不想要那个空白,我能用什么来代替呢Javascript 如何在if语句中不返回任何内容而不返回空白?,javascript,if-statement,Javascript,If Statement,您可以看到,我使用“”使if语句在某些情况下不返回任何内容,但实际上它返回一个空格。我不想要那个空白,我能用什么来代替呢 if (n >= 20000 && n < 100000) { result = `${tens[Math.floor(n / 10000) - 1]} ${ Math.floor((n % 10000) / 1000) != 0 ? num[Math.floor((n % 1
if (n >= 20000 && n < 100000) {
result = `${tens[Math.floor(n / 10000) - 1]} ${
Math.floor((n % 10000) / 1000) != 0
? num[Math.floor((n % 10000) / 1000)]
: ""
} thousand ${n % 1000 != 0 ? number2words(n % 1000) : ""}`;
return result.trim();
}
如果(n>=20000&&n<100000){
结果=`${tens[Math.floor(n/10000)-1]}${
数学楼层((n%10000)/1000)!=0
?数量[数学层((n%10000)/1000)]
: ""
}千${n%1000!=0?个单词(n%1000):“}”;
返回result.trim();
}
要不返回任何内容,请使用return代码>
但是,如果一个case返回一个字符串,您可能应该始终返回一个字符串以保持一致性。当条件为真时,您必须重新构造代码并移动包含空格的字符串
const tensText = tens[Math.floor(n / 10000) - 1];
const divisible = Math.floor((n % 10000) / 1000);
const hundredsText = divisible != 0 ? ` ${num[divisible]}` : '';
const thousandsText = n % 1000 != 0 ? ` ${number2words(n % 1000)}` : '';
result = `${tensText}${hundredsText} thousand${thousandsText}`;
您可以返回null
或false
谢谢!很好用!将部件有条件地推送到数组中,然后加入(“”)
可能会简单得多。欢迎使用@贝基,这是另一个好方法