一,效果圖。 二,代碼。 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript typeof, null, 和 undefined</title> </head> <body> <p> 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>
參考資料:《菜鳥教程》