我有一个 const
numberOfComments
是一个数组的大小,我想有另一个 const
riskByComments
如果 numberOfComments
大于 2,则值为 5,否则应为 0。
我知道我可以使用 let
轻松做到这一点,但我知道最好只使用 const
export const calculateRisk = (ticket) => dispatch => {
const numberOfComments = ticket.comments.length
let riskByComments//if i use const i need to declare its value
//right away, i cannot use an if statement to do it later
if(ticket.comments.length>2){
riskByComments=5//if i use const inside the if statement
}else{
riskByComments=0//i cannot use it in the rest of my function
}
最佳答案
使用条件运算符:
const riskByComments = numberOfComments >2 ? 5 : 0;
关于javascript - 使用 if 语句声明 const 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57072275/