在 JavaScript 中,可以使用 'typeof' 操作符来判断一个变量的数据类型。例如:

typeof 'Hello World'; // 返回 'string'
typeof 42; // 返回 'number'
typeof true; // 返回 'boolean'
typeof undefined; // 返回 'undefined'
typeof null; // 返回 'object'
typeof [1, 2, 3]; // 返回 'object'
typeof { name: 'John', age: 30 }; // 返回 'object'
typeof function() {}; // 返回 'function'

需要注意的是,null 的 typeof 返回的是 'object',这是一个历史遗留问题。同时,typeof 对于函数返回的是 'function',而不是 'object'。

JavaScript 数据类型判断 - typeof 操作符

原文地址: https://www.cveoy.top/t/topic/nwgZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录