JavaScript typeof, null, 和 undefined

typeof 操作符

你可以使用 typeof 操作符来检测变量的数据类型

Null

在 JavaScript 中 null 表示 "什么都没有"。

null是一个只有一个值的特殊类型。表示一个空对象引用。

你可以设置为 undefined 来清空对象。

Undefined

在 JavaScript 中, undefined 是一个没有设置值的变量。

typeof 一个没有值的变量会返回 undefined

任何变量都可以通过设置值为 undefined 来清空。 类型为 undefined.

例子:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <title>typeof undefined null学习</title>
    </head>

    <body>

        <p id="demo"></p>
        <script>
            document.getElementById("demo").innerHTML =
                typeof undefined + "<br>" +
                typeof null + "<br>" +
                (null === undefined) + "<br>" +
                (null == undefined);
        </script>

    </body>

</html>
原文地址:https://www.cnblogs.com/androidsuperman/p/6860966.html