JavaScript type...LOGIN

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中文网(php.cn)</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>
</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中文网(php.cn)</title>
</head>
<body>
<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>
</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中文网(php.cn)</title>
</head>
<body>
<p>对象可以设置为 <b>undefined</b> 来清空。</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中文网(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中文网(php.cn)</title>
</head>
<body>
<p>变量可以通过设置 <b>undefined</b> 来清空。</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中文网(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



Next Section
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</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> </body> </html>
submitReset Code
ChapterCourseware