在 JavaScript 中,有 5 种不同的数据类型可以包含值:
字符串
数字
布尔值
对象
函数
有 6 种类型的对象:
对象
日期
数组
字符串
数字
布尔值
还有 2 种不能包含值的数据类型:
空
未定义
您可以使用 typeof
运算符来查找 a 的数据类型 JavaScript 变量。
typeof "John"
// Returns "string"
typeof 3.14
// Returns "number"
typeof NaN
// Returns "number"
typeof false
// Returns "boolean"
typeof [1,2,3,4] // Returns
"object"
typeof {name:'John', age:34}
// Returns "object"
typeof new Date()
// Returns "object"
typeof function () {} // Returns
"function"
typeof myCar
// Returns "undefined" *
typeof null
// Returns "object"
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The typeof Operator</h2>
<p>The typeof operator returns the type of a variable, object, function or expression:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"'John' is " + typeof "John" + "<br>" +
"3.14 is " + typeof 3.14 + "<br>" +
"NaN is " + typeof NaN + "<br>" +
"false is " + typeof false + "<br>" +
"[1, 2, 3, 4] is " + typeof [1, 2, 3, 4] + "<br>" +
"{name:'John', age:34} is " + typeof {name:'John', age:34} + "<br>" +
"new Date() is " + typeof new Date() + "<br>" +
"function () {} is " + typeof function () {} + "<br>" +
"myCar is " + typeof myCar + "<br>" +
"null is " + typeof null;
</script>
</body>
</html>
请注意:
NaN 的数据类型是数字
数组的数据类型是对象
日期的数据类型是对象
null的数据类型是object
未定义变量的数据类型是未定义 *
未赋值的变量的数据类型是 也未定义 *
您不能使用 typeof
来确定 JavaScript 对象是否是数组(或日期)。
原始数据值是一个简单的数据值,没有附加的 属性和方法。
typeof
运算符可以返回以下原始类型之一:
字符串
数字
布尔值
未定义
typeof "John" // Returns
"string"
typeof 3.14 // Returns
"number"
typeof true // Returns
"boolean"
typeof false // Returns
"boolean"
typeof x
// Returns "undefined" (if x has no value)
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The typeof Operator</h2>
<p>The typeof operator returns the type of a variable or an expression.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof true + "<br>" +
typeof false + "<br>" +
typeof x;
</script>
</body>
</html>
typeof
运算符可以返回两种复杂类型之一:
函数
对象
对于对象、数组和 null,typeof
运算符返回“object”。
typeof
运算符不会为函数返回“object”。
typeof {name:'John', age:34} // Returns "object"
typeof [1,2,3,4]
// Returns "object" (not "array", see note below)
typeof null // Returns
"object"
typeof function myFunc(){} // Returns "function"
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The typeof Operator</h2>
<p>The typeof operator returns object for both objects, arrays, and null.</p>
<p>The typeof operator does not return object for functions.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof {name:'john', age:34} + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof null + "<br>" +
typeof function myFunc(){};
</script>
</body>
</html>
对于数组,typeof
运算符返回“object
”,因为在 JavaScript 中数组是对象。
typeof
运算符不是变量。它是一个运算符。运算符 ( + - * / ) 没有任何数据类型。
但是,typeof
运算符总是返回一个字符串(包含 操作数的类型)。
constructor
属性返回构造函数 所有 JavaScript 变量的函数。
"John".constructor
// Returns function String() {[native code]}
(3.14).constructor
// Returns function Number() {[native code]}
false.constructor // Returns
function Boolean() {[native code]}
[1,2,3,4].constructor
// Returns function Array() {[native code]}
{name:'John',age:34}.constructor
// Returns function Object() {[native code]}
new Date().constructor
// Returns function Date() {[native code]}
function () {}.constructor // Returns
function Function(){[native code]}
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Properties</h1>
<h2>The constructor Property</h2>
<p>The constructor property returns the constructor function for a variable or an
object.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"john".constructor + "<br>" +
(3.14).constructor + "<br>" +
false.constructor + "<br>" +
[1,2,3,4].constructor + "<br>" +
{name:'john', age:34}.constructor + "<br>" +
new Date().constructor + "<br>" +
function () {}.constructor;
</script>
</body>
</html>
您可以检查构造函数属性来确定对象是否是 Array
(包含“数组”一词):
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<p>This "home made" isArray() function returns true when used on an array:</p>
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple"];
document.getElementById("demo").innerHTML = isArray(fruits);
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
</script>
</body>
</html>
或者更简单,您可以检查该对象是否是一个数组函数:
function isArray(myArray) {
return myArray.constructor
=== Array;
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Array Object</h1>
<p>This "home made" isArray() function returns true when used on an array:</p>
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = isArray(fruits);
function isArray(myArray) {
return myArray.constructor === Array;
}
</script>
</body>
</html>
您可以检查构造函数属性来确定对象是否是 日期
(包含单词“日期”):
function isDate(myDate) {
return myDate.constructor.toString().indexOf("Date") > -1;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date Object</h2>
<p>This "home made" isDate() function returns true when used on an date:</p>
<p id="demo"></p>
<script>
const myDate = new Date();
document.getElementById("demo").innerHTML = isDate(myDate);
function isDate(myDate) {
return myDate.constructor.toString().indexOf("Date") > -1;
}
</script>
</body>
</html>
或者更简单,您可以检查对象是否是日期函数:
function isDate(myDate) {
return myDate.constructor === Date;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date Object</h2>
<p>This "home made" isDate() function returns true when used on an date:</p>
<p id="demo"></p>
<script>
const myDate = new Date();
document.getElementById("demo").innerHTML = isDate(myDate);
function isDate(myDate) {
return myDate.constructor === Date;
}
</script>
</body>
</html>
在 JavaScript 中,没有值的变量的值为 undefined
。 该类型也是未定义
。
let car; // Value is undefined,
type is undefined
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The typeof Operator</h2>
<p>The value (and the data type) of a variable with no value is <b>undefined</b>.</p>
<p id="demo"></p>
<script>
let car;
document.getElementById("demo").innerHTML =
car + "<br>" + typeof car;
</script>
</body>
</html>
通过将值设置为 undefined
,可以清空任何变量。 该类型也将是未定义
。
car = undefined; // Value is undefined,
type is undefined
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The typeof Operator</h2>
<p>Variables can be emptied if you set the value to <b>undefined</b>.</p>
<p id="demo"></p>
<script>
let car = "Volvo";
car = undefined;
document.getElementById("demo").innerHTML = car + "<br>" + typeof car;
</script>
</body>
</html>
空值与 undefined
无关。
空字符串同时具有合法值和类型。
let car = ""; //
The value is
"", the typeof is "string"
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p>An empty string has both a legal value and a type:</p>
<p id="demo"></p>
<script>
let car = "";
document.getElementById("demo").innerHTML =
"The value is: " +
car + "<br>" +
"The type is: " + typeof car;
</script>
</body>
</html>
在 JavaScript 中 null
表示“无”。它应该是不存在的东西。
不幸的是,在 JavaScript 中,null
的数据类型是对象。
您可以将 typeof null
视为对象视为 JavaScript 中的错误。它应该是null
。
您可以通过将对象设置为 null
来清空该对象:
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null; //
Now value is null,
but type is still an object
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p>Objects can be emptied by setting the value to <b>null</b>.</p>
<p id="demo"></p>
<script>
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null;
document.getElementById("demo").innerHTML = typeof person;
</script>
</body>
</html>
您还可以通过将对象设置为 undefined
来清空对象:
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined; //
Now both value and type is undefined
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Objects</h1>
<h2>The undefined Data Type</h2>
<p>Objects can be emptied by setting the value to <b>undefined</b>.</p>
<p id="demo"></p>
<script>
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined;
document.getElementById("demo").innerHTML = person;
</script>
</body>
</html>
undefined
和 null
值相同,但类型不同:
typeof undefined
// undefined
typeof null
// object
null === undefined
// false
null == undefined
// true
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The typeof Operator</h2>
<p>Undefined and null are equal in value but different in type:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof undefined + "<br>" +
typeof null + "<br><br>" +
(null === undefined) + "<br>" +
(null == undefined);
</script>
</body>
</html>
如果对象是指定对象的实例,则 instanceof
运算符返回 true
:
const cars = ["Saab", "Volvo", "BMW"];
(cars instanceof Array);
(cars instanceof Object);
(cars instanceof String);
(cars instanceof Number);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The instanceof Operator</h2>
<p>The instanceof operator returns true if an object is an instance of a specified object:</p>
<p id="demo"></p>
<script>
const cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML =
(cars instanceof Array) + "<br>" +
(cars instanceof Object) + "<br>" +
(cars instanceof String) + "<br>" +
(cars instanceof Number);
</script>
</body>
</html>
void 运算符计算表达式并返回 未定义。该运算符常用于获取未定义的 原始值,使用“void(0)”(在评估不带任何值的表达式时很有用) 使用返回值)。
<a href="javascript:void(0);">
Useless link
</a>
<a href="javascript:void(document.body.style.backgroundColor='red');">
Click me to change the background color of body to red
</a>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The void Operator</h2>
<p>
<a href="javascript:void(0);">Useless link</a>
</p>
<p>
<a href="javascript:void(document.body.style.backgroundColor='red');">
Click me to change the background color of body to red.</a>
</p>
</body>
</html>