Like most programming languages, there is a boolean type in JavaScript for logical judgment. However, unlike many other programming languages, there is the concept of Truthy and Falsy values in JavaScript - except for boolean values true and false, all types of JavaScript values can be used for logical judgment, and the rules are as follows:
1. All Falsy values are false when logical judgment is made. Falsy values include: false, undefined, null, plus or minus 0, NaN, "".
2. All other values are Truthy, and are true when making logical judgments. It is worth noting that Infinity, empty array, and "0" are all Truthy values.
experiment
The code copy is as follows:
var x = "0";
if(x){
"string 0 is Truthy."
} else {
"string 0 is Falsy."
}
var y = [];
if(y){
"empty array is Truthy."
} else {
"empty array is Falsy."
}