一个正在努力爱好运动的程序猿-座右铭:越努力越幸运,越运动越健康,热爱编程,热爱运动。
基本类型: number string undefined null boolean 属于栈类型 存值
typeof(1) === "number" typeof('1')=== "string" typeof(a) === "undefined" typeof(null) ==="object" typeof(true) === "boolean" /* *typeof 返回的数据类型6种 (1)number; (2)string; (3)boolean; (4)object; (5)function; (6)undefined; */引用类型: array object function 属于堆类型 存地址
代码如下(示例):
/* *主要讲typeof的各种返回值 *typeof:操作符返回一个字符串,表示未经计算的操作数的类型。 *typeof语法:typeof或者typeof() 有无括号都可以 */ //1.number typeof(37) === "number"; typeof(3.14) === "number"; //Math.LN2 属性表示 2 的自然对数,约为 0.693: ==Ln(2) typeof(Math.LN2) === "number"; typeof(Infinity) === "number"; typeof(NaN) === "number"; typeof(Number(1)) === "number"; //不要使用这种形式 //2.string typeof("") === "string"; typeof("a") === "string"; typeof(typeof 1) === "string"; typeof(String("abc")) === "string"; //不要使用这种形式 //3.boolean typeof(true) === "boolean"; typeof(false) === "boolean"; typeof(Boolean(true)) === "boolean"; //不要使用这种形式 //4.underfined typeof(未赋值的变量) ==="underfined" //5.object typeof({}) ==="object" typeof([]) ==="object" //区分数组和对象使用 Array.isArray 或者 Object.prototype.toString.call typeof(new Date())==="object" //让人产生迷惑的 typeof new Boolean(true)==="object"; typeof new Number(1)==="object"; typeof new String("1")==="object"; //6.function typeof(function(){}) === "function" typeof(class x{}) === "function" typeof(Math.sin) === "function" typeof(new Function) === "function"原文详细看这里----------
