Falsy VS Truthy Value and == VS ===

  • Falsy values: undefined, null, 0, '', NaN
  • Truthy values: Not falsy values
var height;

    if (height) {
console.log('Variable is defined');
} else {
console.log('Variable has NOT been defined');
}

In this code above, the result is: Variable has NOT been defined, because height is undefined -- falsy value

So, if we insert height = 23 before if, height will become a truthy value. The result will be Variable is defined

	var height;
height = 23;
if (height) {
console.log('Variable is defined');
} else {
console.log('Variable has NOT been defined');
}

But again, if height = 0; , it will return Variable has NOT been defined

    var height;
height = 0;
if (height) {
console.log('Variable is defined');
} else {
console.log('Variable has NOT been defined');
}

Next I will talke about the ||, == and ===.

   var height;
height = 0;
if (height || height === 0) { // height == 0)
console.log('Variable is defined');
} else {
console.log('Variable has NOT been defined');
}

"||" means "or". Therefore, if will check the two conditions, if one of the condition is met, it will console.log 'Variable is defined'. Here, height === 0, so it returns Variable is defined.

Operation == is called "lenient" or "normal" equality. == only compares the value, it does not compair the type of value.

Operation === is called “strict” or “identical” equality. === compares the value and type. if var a=0, and int b=0, a=b returns false, because the type is different.

    console.log(23 == '23')  //--- ture
console.log(23 === '23') // ---false

最新文章

  1. git: 修改commiter 信息
  2. DOM相关知识点以及原型
  3. putty快速设置本地代理
  4. Python操作excel,及图表展示
  5. php header 函数详解
  6. 【BZOJ】【3205】【APIO2013】机器人robot
  7. linux内存——/proc/sys/vm/drop_caches
  8. Activity 的生命周期与加载模式
  9. win7 变WIFI热点 & 在线Linux 内核代码
  10. Android消息机制之Handler
  11. 5. SQL Server数据库性能监控 - 当前请求
  12. HTML中的表单
  13. xmlString和map互转Util
  14. winfrom SVG转Imge
  15. ASP.NET Core OData now Available
  16. 伙伴系统之避免碎片--Linux内存管理(十六)
  17. 【Mac】-NO.133.Mac.1 -【重置忘记macos root密码】
  18. jquery简易tab切换
  19. Django开启国际化的支持
  20. vc++使用cookie登录网站

热门文章

  1. 【实战】yolov8 tensorrt模型加速部署
  2. pycharm编辑器下载与使用
  3. WordPress4.6任意命令执行漏洞
  4. 支付对接常用的加密方式介绍以及java代码实现
  5. 【一句话】 OAuth 2
  6. MySQL的简单安装配置
  7. sqlserver数据库批量新增修改类
  8. 学习Java Day17
  9. ajax的原理是什么?如何实现?
  10. JZOJ 1166. 树中点对距离