邵阳公司网站建设,数字网站建设,全国建筑行业资质平台查询,中国知名设计网站1. typeof
typeof 用于判断一个变量的数据类型#xff0c;返回一个表示数据类型的字符串。可以对如下类型进行判断#xff1a;undefined、boolean、number、string、bigint、symbol、function 和 object。对 null 的判断是object#xff0c;这是个历史遗留问题。
typeof n…1. typeof
typeof 用于判断一个变量的数据类型返回一个表示数据类型的字符串。可以对如下类型进行判断undefined、boolean、number、string、bigint、symbol、function 和 object。对 null 的判断是object这是个历史遗留问题。
typeof null; // object
typeof undefined; // undefined
typeof true; // boolean
typeof 110; // number
typeof yqcoder; // string
typeof function () {}; // function
typeof {}; // object
typeof []; // object
typeof 9007199254740999n; // bigint
typeof Symbol(); // symbol
2. instanceof
instanceof 用于判断一个对象是否属于某个类或者其父类的实例。如果对象是指定类的实例则返回 true否则返回 false。
const time new Date();
const reg /^yqcoder$/;time instanceof Date; // true
reg instanceof RegExp; // true
综上typeof 用于判断基本数据类型和函数类型而 instanceof 用于判断对象是否属于某个类的实例。