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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值