function formatToStandardizedDate(from, to){
const from_date = moment(from);
if(to){
let to_date = moment(to);
}else{
let to_date = null;
}
}
console.log(formatToStandardizedDate("2017-04-19 00:00:00",null))
我的代碼出現了什麼問題?如果to
爲空,它至少會將空值分配給to_date
,但我得到了未定義錯誤的to_date
錯誤。爲什麼?讓變量沒有定義
讓你不能使用同名的var。 – Jai
[使用「let」和「var」聲明變量有什麼區別?](http://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and- var-to-declare-a-variable) – mxr7350
如果你想在塊外使用它,你需要在塊外定義'let'。 –