JavaScript: typeof operator

typeof null === 'object'
若想檢查某個項目是否為 object,而且要排除 null,可以寫:
if (待檢查項目 && (typeof 待檢查項目 === 'object' || typeof 待檢查項目 === 'function')) {
  console.log('This is an object.')
} else {
  console.log('This is not an object!')
}

typeof NaN === 'number'
若想檢查某個項目是否為 number,而且要排除 NaN,可以寫:
if (Number.isNaN(待檢查項目) === false && typeof 待檢查項目 === 'number') {
  console.log('This is a number.')
} else {
  console.log('This is not a number!')
}


其他相關參考資料: 

重新認識 JavaScript: Day 03 變數與資料型別

Comments

Popular posts from this blog

Alpha Camp 全端開發課程學習心得

在 javascript 用 regular expression 為金額加上千位數分隔符號

shop_platform - sqlalchemy.exc.TimeoutError