首先我们知道undefined与null都属于 javascript 的 7 种基本类型。
(推荐教程:js教程)
let primitiveTypes = ['string','number','null','undefined','boolean','symbol', 'bigint'];
它们是属于虚值,可以使用Boolean(value)或!!value将其转换为布尔值时,值为false。
console.log(!!null); // false console.log(!!undefined); // false console.log(Boolean(null)); // false console.log(Boolean(undefined)); // false
区别:
undefined是未指定特定值的变量的默认值,或者没有显式返回值的函数,如:console.log(1),还包括对象中不存在的属性,这些 JS 引擎都会为其分配 undefined 值。
null是“不代表任何值的值”。 null是已明确定义给变量的值。 在此示例中,当fs.readFile方法未引发错误时,我们将获得null值。
在比较null和undefined时,我们使用==时得到true,使用===时得到false。
console.log(null == undefined); // true console.log(null === undefined); // false
以上就是undefined与null有什么区别的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号