我们可以使用 typeof 操作符来查看 JavaScript 变量的数据类型。而constructor 属性返回所有 JavaScript 变量的构造函数。
typeof实例: (可以看到,typeof 遇到 null,数组,对象时,都只返回 object 类型)
typeof"John"
// 返回 string
typeof3.14 // 返回 number
typeof NaN // 返回 number
typeoffalse // 返回 boolean
typeof [1,2,3,4] // 返回 object
typeof {name:'John', age:34} // 返回 object
typeofnew Date() // 返回 object
typeoffunction () {} // 返回 function
typeof myCar // 返回 undefined (如果 myCar 没有声明)
typeofnull // 返回 object
typeof3.14 // 返回 number
typeof NaN // 返回 number
typeoffalse // 返回 boolean
typeof [1,2,3,4] // 返回 object
typeof {name:'John', age:34} // 返回 object
typeofnew Date() // 返回 object
typeoffunction () {} // 返回 function
typeof myCar // 返回 undefined (如果 myCar 没有声明)
typeofnull // 返回 object
constructor实例:
"John".constructor
// 返回函数 String() { [native code] }
(3.14).constructor // 返回函数 Number() { [native code] }
false.constructor // 返回函数 Boolean() { [native code] }
[1,2,3,4].constructor // 返回函数 Array() { [native code] }
{name:'John', age:34}.constructor // 返回函数 Object() { [native code] }
new Date().constructor // 返回函数 Date() { [native code] }
function () {}.constructor // 返回函数 Function(){ [native code] }
(3.14).constructor // 返回函数 Number() { [native code] }
false.constructor // 返回函数 Boolean() { [native code] }
[1,2,3,4].constructor // 返回函数 Array() { [native code] }
{name:'John', age:34}.constructor // 返回函数 Object() { [native code] }
new Date().constructor // 返回函数 Date() { [native code] }
function () {}.constructor // 返回函数 Function(){ [native code] }
判断某个变量是否是某个对象的实例则要选择使用另一个关键语法 instanceof
可通过 instanceof 操作符来判断对象的具体类型,语法格式:
var result = objectName instanceof objectType
返回布尔值,如果是指定类型返回 true,否则返回 false:
例:
arr = [1,2,3];
if(arr instanceof Array){ //true
document.write("arr 是一个数组");
} else {
document.write("arr 不是一个数组");
}
下面补充一些JavaScript里数据类型转换的例子
当 JavaScript 尝试操作一个 "错误" 的数据类型时,会自动转换为 "正确" 的数据类型。
5 +null // 返回 5 null 转换为 0
"5" +null // 返回"5null" null 转换为 "null"
"5" +1 // 返回 "51" 1 转换为 "1"
"5" - 1 // 返回 4 "5" 转换为 5
"5" +null // 返回"5null" null 转换为 "null"
"5" +1 // 返回 "51" 1 转换为 "1"
"5" - 1 // 返回 4 "5" 转换为 5
自动转换为字符串
当你尝试输出一个对象或一个变量时 JavaScript 会自动调用变量的 toString() 方法:
document.getElementById(
"demo").innerHTML = myVar;
// if myVar = {name:"Fjohn"} // toString 转换为 "[object Object]"
// if myVar = [1,2,3,4] // toString 转换为 "1,2,3,4"
// if myVar = new Date() // toString 转换为 "Sun Dec 24 2017 15:08:55 GMT+0200"
// if myVar = {name:"Fjohn"} // toString 转换为 "[object Object]"
// if myVar = [1,2,3,4] // toString 转换为 "1,2,3,4"
// if myVar = new Date() // toString 转换为 "Sun Dec 24 2017 15:08:55 GMT+0200"
数字和布尔值也经常相互转换:
// if myVar = 123 // toString 转换为 "123"
// if myVar = true // toString 转换为 "true"
// if myVar = false // toString 转换为 "false"
// if myVar = true // toString 转换为 "true"
// if myVar = false // toString 转换为 "false"
将数字转换为字符串
全局方法 String() 可以将数字转换为字符串。该方法可用于任何类型的数字,字母,变量,表达式:
var x = 123;
String(x) // 将变量 x 转换为字符串并返回 "123"
String(123) // 将数字 123 转换为字符串并返回 "123"
String(100 + 23) // 将数字表达式转换为字符串并返回 "123"
String(123) // 将数字 123 转换为字符串并返回 "123"
String(100 + 23) // 将数字表达式转换为字符串并返回 "123"
Number 方法 toString() 也是有同样的效果。
x.toString() //"123"
(123).toString() //"123"
(100 + 23).toString() //"123"
(123).toString() //"123"
(100 + 23).toString() //"123"
将字符串转换为数字
全局方法 Number() 可以将字符串转换为数字,注:空字符串转换为 0。其他的字符串会转换为 NaN (不是个数字)。
Number("3.14") // 返回 3.14
Number(" ") // 返回 0
Number("") // 返回 0
Number("99 88") // 返回 NaN
Number(" ") // 返回 0
Number("") // 返回 0
Number("99 88") // 返回 NaN