简体   繁体   English

如何在 {} 括号中使用循环和条件?

[英]How to use loops and conditionals in {} brackets?

I am not able to use if and for loop inside {} why?我无法在 {} 中使用 if 和 for 循环,为什么? Anything wrong?哪里不对了?

sendTextMessageWithPromise(assignerID, `Value : ${if(x==0) console.log(x)}`)

These are called template literals .这些被称为模板文字 And you can't use if statements or for loops inside them.而且您不能在其中使用if语句或for循环。 What you can do is a conditional check with ternary operator .您可以做的是使用三元运算符进行条件检查。

That's how you can do it:你可以这样做:

console.log(`Answer: ${ (age == 18)? 'age is 18' : 'age is not 18' }`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM