【代码笔记】Web-Javascript-Javascript typeof
一,效果图。
二,代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript typeof, null, 和 undefined</title>
</head>
<body>
<p> typeof 操作符返回变量或表达式的类型。</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof false + "<br>" +
typeof [1, 2, 3, 4] + "<br>" +
typeof {
name: 'john',
age: 34
};
</script>
<p>对象可以通过设置为<b>null</b>来清空</p>
<p id="demo"></p>
<script>
var person = {
firstName: "john",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
var person = null;
document.getElementById("demo").innerHTML = typeof person;
</script>
<p>变量的值如果不存在则改变量的值为<b>undefined</b></p>
<p id="demo"></p>
<script>
var person;
document.getElementById("demo").innerHTML = person + "<br>" + typeof person;
</script>
<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/yang-guang-girl/p/10172103.html