×

Js的基础类型:typeof和instanceof的区别

提问者:abc1232023.07.19浏览:850

基础类型有:boolean、string、number、bigint、undefined、symbol、null。

typeof能识别所有的值类型,识别函数,能区分是否是引用类型。

const a = "str";
console.log("typeof a :>> ", typeof a); // typeof a :>>  string

const b = 999;
console.log("typeof b :>> ", typeof b); // typeof b :>>  number

const c = BigInt(9007199254740991);
console.log("typeof c :>> ", typeof c); // typeof c :>>  bigint

const d = false;
console.log("typeof d :>> ", typeof d); // typeof d :>>  boolean

const e = undefined;
console.log("typeof e :>> ", typeof e); // typeof e :>>  undefined

const f = Symbol("f");
console.log("typeof f :>> ", typeof f); // typeof f :>>  symbol

const g = null;
console.log("typeof g :>> ", typeof g); // typeof g :>>  object

const h = () => {};
console.log("typeof h :>> ", typeof h); // typeof h :>>  function

const i = [];
console.log("typeof i :>> ", typeof i); // typeof i :>>  object

instanceof用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

您的支持是我们创作的动力!

网友回答文明上网理性发言 已有0人参与

发表评论: