JavaScript typeof



JavaScript typeof, null, undefined, valueOf().


typeof operator

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

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>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>

Run Instance»

Click the "Run Instance" button to view the online instance

Note In JavaScript, an array is a special object type. Therefore typeof [1,2,3,4] Return object.

Null

In JavaScript, null means "nothing".

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

NoteUse 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.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 Instance»

Click the "Run Instance" button to view the online instance

You can set it to undefined to clear the object:

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>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 Instance»

Click the "Run Instance" button to view the online instance


Undefined

at In JavaScript, undefined is a variable with no set value.

typeof A variable with no value returns undefined.

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>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 Instance»

Click the "Run Instance" button to view the online instance

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.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 Instance»

Click "Run Instance" Button to view online examples


The difference between Undefined and Null

Examples

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>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 Example»

Click the "Run Instance" button to view the online instance