LEATH
您正在寻找的是 Javascript 中的“强制”。当我们在需要其他类型变量的地方/函数/条件等中使用一种类型的 Javascript 变量时,Javascript 不会抛出错误。相反,它将该变量的值更改为该特定类型的变量。这叫胁迫。例如:var a = "" ;if (a){ //a is coerced to false console.log (true);}else{console.log (false);}在上面的代码中,空字符串被强制为false。同样,在您的代码中,正在发生强制:var x=1if(x){ // x is coerced to true, this condition is met and x is set to 0. x=0;}if(x){ // since x is 0, it is coerced to false, so condition is not satisfied x=1};alert(x);有关更多详细信息,请查看此链接。