JavaScript 判断是否为空

雨点打透心脏的1/2处 2021-11-22 05:08 675阅读 0赞
  1. // var a = "";
  2. // var a = " ";
  3. // var a = null;
  4. // var a = undefined;
  5. // var a = [];
  6. // var a = {};
  7. // var a = NaN;
  8. if(a === undefined) { // 只能用 === 运算来测试某个值是否是未定义的
  9. console.log("为undefined");
  10. }
  11. if(a == null) { // 等同于 a === undefined || a === null
  12. console.log("为null");
  13. }
  14. // String
  15. if(a == "" || a == null || a == undefined){ // "",null,undefined
  16. console.log("为空");
  17. }
  18. if(!a){ // "",null,undefined,NaN
  19. console.log("为空");
  20. }
  21. if(!$.trim(a)){ // "",null,undefined
  22. console.log("为空");
  23. }
  24. // Array
  25. if(a.length == 0){ // "",[]
  26. console.log("为空");
  27. }
  28. if(!a.length){ // "",[]
  29. console.log("为空");
  30. }
  31. // Object {}
  32. if($.isEmptyObject(a)){ // 普通对象使用 for...in 判断,有 key 即为 false
  33. console.log("为空");
  34. }

转载于:https://www.cnblogs.com/sea-stream/p/11100392.html

发表评论

表情:
评论列表 (有 0 条评论,675人围观)

还没有评论,来说两句吧...

相关阅读