国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

JavaScript typeof

JavaScript typeof, null, undefined, valueOf().


##typeof operator

You can use the typeof operator to detect the data type of a variable.

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p> typeof 操作符返回變量或表達(dá)式的類型。</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>
</body>
</html>

Tips: In JavaScript, an array is a special object type. So typeof [1,2,3,4] returns object.


Null

In JavaScript, null means "nothing".

null is a special type with only one value. Represents an empty object reference.

Use typeof to detect null and return object.

You can set it to null to clear the object:

Instance

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>對(duì)象可以通過(guò)設(shè)置為 <b>null</b> 來(lái)清空。</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>
</body>
</html>

Run the program to try it


You can set it to undefined to clear the object:


Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>對(duì)象可以設(shè)置為 <b>undefined</b> 來(lái)清空。</p>
<p id="demo"></p>
<script>
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
var person = undefined;
document.getElementById("demo").innerHTML = typeof person;
</script>
</body>
</html>

Run the program and try it


Undefined

In JavaScript, undefined is a variable that has no set value.

typeof A variable with no value will return undefined.

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>變量的值如果不存在則該變量值為 <b>undefined</b>。</p>
<p id="demo"></p>
<script>
var person;
document.getElementById("demo").innerHTML =
person + "<br>" + typeof person;
</script>
</body>
</html>

Run the program to try it


Any variable can be cleared by setting the value to undefined. The type is undefined.

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>變量可以通過(guò)設(shè)置 <b>undefined</b> 來(lái)清空。</p>
<p id="demo"></p>
<script>
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
var person = undefined;
document.getElementById("demo").innerHTML =
person + "<br>" + typeof person;
</script>
</body>
</html>

Run the program and try it


Undefined The difference with Null

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文網(wǎng)(php.cn)</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>

Run the program and try it



Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鳥教程(runoob.com)</title> </head> <body> <p> typeof 操作符返回變量或表達(dá)式的類型。</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> </body> </html>
submitReset Code