当前位置: 代码迷 >> 综合 >> js 假值
  详细解决方案

js 假值

热度:14   发布时间:2023-10-08 20:32:53.0
// JavaScript拥有数量奇大的假值
function falsyValue() {document.writeln(0); // 0document.writeln(0 === false); // false/***  假值范围 :*      0 NaN '' false  null undefined* */if (0) {document.writeln(1);} else {document.writeln("假值"); // 输出 假值}if (NaN) {document.writeln(1);} else {document.writeln("假值"); // 输出 假值}if ('') {document.writeln(1);} else {document.writeln("假值"); // 输出 假值}if (false) {document.writeln(1);} else {document.writeln("假值"); // 输出 假值}if (null) {document.writeln(1);} else {document.writeln("假值"); // 输出 假值}if (undefined) {document.writeln(1);} else {document.writeln("假值"); // 输出 假值}/*** undefined 和 NaN并不是常量。它们居然是全局变量,而且你可以改变它的值,那本不是应该的,但事实确实如此* 。千万别这样做*/undefined = 1;document.writeln(undefined == 1);  // falsedocument.writeln(undefined === 1); // falseundefined = false;document.writeln(undefined === false); // false
}

  相关解决方案