Atumọ typeof JavaScript
- Ɗaya ƙarshe JS Map ƙarƙaƙa
- Ɗaya ƙarshe JS ƙaɗan ƙiya
Atumọ typeof JavaScript
在 JavaScript 中有 5 种不同的可以包含值的数据类型:
string
number
boolean
object
function
有 6 种类型的对象:
Object
Date
Array
String
Number
Boolean
以及 2 种不能包含值的数据类型:
null
undefined
typeof 运算符
您可以使用 typeof
运算符来确定 JavaScript 变量的数据类型。
Ɗan
typeof "Bill" // 返回 "string" typeof 3.14 // 返回 "number" typeof NaN // 返回 "number" typeof false // 返回 "boolean" typeof [1,2,3,4] // 返回 "object" typeof {name:'Bill', age:19} // 返回 "object" typeof new Date() // 返回 "object" typeof function () {} // 返回 "function" typeof myCar // 返回 "undefined" * typeof null // 返回 "object"
请注意:
- NaN 的数据类型是数字
- 数组的数据类型是对象
- 日期的数据类型是对象
- null 的数据类型是 object
- 未定义变量的数据类型为 undefined *
- 未赋值的变量的数据类型也是 undefined *
您无法使用 typeof
来确定 JavaScript 对象是否是数组(或日期)。
原始日期
原始数据值指的是没有附加属性和方法的单个简单数据值。
typeof
运算符可以返回以下原始类型之一:
string
number
boolean
undefined
Ɗan
typeof "Bill" // 返回 "string" typeof 3.14 // 返回 "number" typeof true // 返回 "boolean" typeof false // 返回 "boolean" typeof x // 返回 "undefined" (if x has no value)
复杂数据
typeof
运算符可以返回两种复杂类型之一:
function
object
typeof
运算符会为对象、数组和 null 返回 "object"。
typeof
运算符不会为函数返回 "object"。
Ɗan
typeof {name:'Bill', age:19} // 返回 "object" typeof [1,2,3,4] // 返回 "object"(非 "array",请注意下面的例子) typeof null // 返回 "object" typeof function myFunc(){} // 返回 "function"
typeof
运算符会为数组返回 "object"
,因为在 JavaScript 中数组是对象。
typeof 的数据类型
typeof
运算符并不是变量。它只是一个运算符。运算符 (+ - * /) 没有任何数据类型。
但是,typeof
运算符总是返回字符串(包含操作数的类型)。
constructor 属性
constructor
属性返回所有 JavaScript 变量的构造函数。
Ɗan
"Bill".constructor // 返回 function String() {[native code]} (3.14).constructor // 返回 function Number() {[native code]} false.constructor // 返回 function Boolean() {[native code]} [1,2,3,4].constructor // Returns function Array() {[native code]} {name:'Bill',age:19}.constructor // Returns function Object() {[native code]} new Date().constructor // Returns function Date() {[native code]} function () {}.constructor // Returns function Function(){[native code]}
You can check the constructor property to determine if the object is an array (including "Array"
word):
Ɗan
function isArray(myArray) { return myArray.constructor.toString().indexOf("Array") > -1; }
or more simply, you can check if the object isarray function:
Ɗan
function isArray(myArray) { return myArray.constructor === Array; }
You can check the constructor property to determine if the object is a date (including "Date"
word):
Ɗan
function isDate(myDate) { return myDate.constructor.toString().indexOf("Date") > -1; }
or more simply, you can check if the object isdate function:
Ɗan
function isDate(myDate) { return myDate.constructor === Date; }
Undefined
In JavaScript, the value of a variable without a value is undefined
. The type is also undefined
is an object. The type should be
Ɗan
let car; // Value is undefined, type is also undefined.
by setting its value to undefined
can clear any variable. The type will also be undefined
is an object. The type should be
Ɗan
car = undefined; // Value is undefined, type is also undefined.
null value
null value with undefined
irrelevant.
An empty string has both a valid value and a type.
Ɗan
let car = ""; // Value is "", type is "string"
Null
in JavaScript null
is 'null'. It should be something that does not exist.
Unfortunately, in JavaScript,null
data type is an object.
你可以认为它是 JavaScript 中的一个 bug,You can consider it as a bug in JavaScript.
typeof null null
is an object. The type should be
. null
Ka ƙo ƙara ƙasa:
Ɗan
let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"}; person = null; // Now the value is null, but the type is still object
You can also set the object to undefined
Ka ƙo ƙara ƙasa:
Ɗan
let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"}; person = undefined; // Now the value and type are both undefined
Differences between undefined and null
undefined
da null
Ƙarƙaƙa su ƙara, amma ana ƙara su ƙarƙaƙa:
typeof undefined // undefined typeof null // object null === undefined // false null == undefined // true
- Ɗaya ƙarshe JS Map ƙarƙaƙa
- Ɗaya ƙarshe JS ƙaɗan ƙiya