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