Truthy and Falsy Values and Equality Operators

时间:2023-08-13 16:24:44

  最近在学习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")
}