当前位置: 首页 > 工具软件 > falsy > 使用案例 >

Truthy and Falsy Values and Equality Operators

吕向荣
2023-12-01

  最近在学习JS.  有一些概念还希望跟大家分享出来

 

Truthy and Falsy Values

 

Falsy Values: undefined, null, 0, '', NaN(not a number)

Truthy values: NOT falsy values

 

var height;
    height = 0; // return else value

    var name;
    name = ''; // return else value

    var testNumber;
    testNumber = 1; // return if value

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

 

Equality Operators

== 和 === 的区别

 

=== 是必须type 相同的情况下value 相同.

 

e.g. 

 

var number1;
    number1 = 1;

    var number2;
    number2 = 1;

    var isNumberSame;
    number1 === number2 ? isNumberSame = true : isNumberSame = false;
    console.log("Is 2 numbers same? " + isNumberSame)

 

== 不考虑type的情况下只考虑value相同.

 

e.g.

var number1;
    number1 = 1;

    var number2;
    number2 = 1;

    var string1;
    string1 = "1";

    // var isNumberSame;
    // number1 === number2 ? isNumberSame = true : isNumberSame = false;
    // console.log("Is 2 numbers same? " + isNumberSame)



    if(number1 == string1)
    {
        console.log("The == operator does type ")
    }
    else{
        console.log("Error")
    }

 

转载于:https://www.cnblogs.com/TheMiao/p/9734380.html

 类似资料:

相关阅读

相关文章

相关问答