类型 |
原始值(String,Number,Boolean,null,undefined,Symbol) |
引用值(Object,Array,Function) |
存储 |
栈内存中,占用固定大小空间 |
堆内存中 |
复制区别 |
独立拷贝,不会相互影响 |
共享对象,新旧变量会指向同一个对象 |
动态修改 |
无法修改 |
可以随时修改,添加,删除 |
类型检查 |
typeof |
instanceof |
类型检测typeof
主要用于原始数据类型(string, number, boolean, undefined, symbol)检测
// 原始类型
// undefined
console.log(typeof undefined); // "undefined"
console.log(typeof undeclaredVariable); // "undefined"
// boolean
console.log(typeof true); // "boolean"
console.log(typeof false); // "boolean"
console.log(typeof Boolean(1)); // "boolean"
// number
console.log(typeof 42); // "number"
console.log(typeof 3.14); // "number"
console.log(typeof NaN); // "number"
console.log(typeof Infinity); // "number"
console.log(typeof -Infinity); // "number"
// string
console.log(typeof "hello"); // "string"
console.log(typeof 'world'); // "string"
console.log(typeof `template`); // "string"
// bigint
console.log(typeof 123n); // "bigint"
console.log(typeof BigInt(123)); // "bigint"
// symbol
console.log(typeof Symbol()); // "symbol"
console.log(typeof Symbol('description')); // "symbol"
// object
console.log(typeof {}); // "object"
console.log(typeof []); // "object" (注意:数组也是对象)
console.log(typeof null); // "object" (这是JavaScript的一个bug)
console.log(typeof new Date()); // "object"
console.log(typeof new RegExp()); // "object"
console.log(typeof new Error()); // "object"
// function
console.log(typeof function() {}); // "function"
console.log(typeof () => {}); // "function"
console.log(typeof class {}); // "function"
类型检测instanceof
检查对象是否是某个构造函数的实例
// 原始类型
console.log("hello" instanceof String); // false
console.log(42 instanceof Number); // false
console.log(true instanceof Boolean); // false
// 检查数组
const arr = [1, 2, 3];
console.log(arr instanceof Array); // true
console.log(arr instanceof Object); // true (数组也是对象)
// 检查字符串
const str = "hello";
console.log(str instanceof String); // false (原始类型)
console.log(new String("hello") instanceof String); // true
// 检查数字
const num = 42;
console.log(num instanceof Number); // false (原始类型)
console.log(new Number(42) instanceof Number); // true
// 检查布尔值
const bool = true;
console.log(bool instanceof Boolean); // false (原始类型)
console.log(new Boolean(true) instanceof Boolean); // true
//检查函数类型
function myFunction() {}
const arrowFunction = () => {};
console.log(myFunction instanceof Function); // true
console.log(arrowFunction instanceof Function); // true
console.log(Array instanceof Function); // true
项目常用写法
function getTypeInfo(value) {
const type = typeof value;
switch (type) {
case 'undefined':
return '未定义的值';
case 'boolean':
return '布尔值';
case 'number':
return isNaN(value) ? '非数字' : '数字';
case 'string':
return '字符串';
case 'bigint':
return '大整数';
case 'symbol':
return '符号';
case 'object':
if (value === null) return '空值';
if (Array.isArray(value)) return '数组';
if (value instanceof Date) return '日期';
if (value instanceof RegExp) return '正则表达式';
return '对象';
case 'function':
return '函数';
default:
return '未知类型';
}
}
console.log(getTypeInfo(42)); // "数字"
console.log(getTypeInfo("hello")); // "字符串"
console.log(getTypeInfo([])); // "数组"
console.log(getTypeInfo(null)); // "空值"
console.log(getTypeInfo(undefined)); // "未定义的值"
function validateParameters(name, age, isActive) {
const errors = [];
if (typeof name !== 'string') {
errors.push('姓名必须是字符串');
}
if (typeof age !== 'number' || isNaN(age)) {
errors.push('年龄必须是有效数字');
}
if (typeof isActive !== 'boolean') {
errors.push('激活状态必须是布尔值');
}
return errors;
}
console.log(validateParameters("张三", 25, true)); // []
console.log(validateParameters(123, "25", "true"));