JavaScriptでは、プリミティブ(ブール値、null、数値、文字列、および値undefined(およびES6のシンボル))を除いて、すべてがオブジェクトです(または少なくともオブジェクトとして扱われます)。
console.log(typeof true);           // boolean
console.log(typeof 0);              // number
console.log(typeof "");             // string
console.log(typeof undefined);      // undefined
console.log(typeof null);           // object
console.log(typeof []);             // object
console.log(typeof {});             // object
console.log(typeof function () {}); // function
オブジェクトを見るとわかるように、配列と値nullはすべてオブジェクトと見なされます(null存在しないオブジェクトへの参照です)。関数は、呼び出し可能なオブジェクトの特殊なタイプであるため、区別されます。ただし、それらはまだオブジェクトです。
一方リテラルtrue、0、""およびundefinedオブジェクトではありません。これらはJavaScriptのプリミティブな値です。ただし、ブール値、数値、および文字列にもコンストラクターBooleanがNumberありString、それぞれのプリミティブをラップして追加の機能を提供します。
console.log(typeof new Boolean(true)); // object
console.log(typeof new Number(0));     // object
console.log(typeof new String(""));    // object
ご覧のとおり、プリミティブ値がBoolean、NumberおよびStringコンストラクター内でそれぞれラップされると、それらはそれぞれオブジェクトになります。instanceofオペレータは(それが返す理由であるオブジェクトのために働くfalseプリミティブ値のために):
console.log(true instanceof Boolean);              // false
console.log(0 instanceof Number);                  // false
console.log("" instanceof String);                 // false
console.log(new Boolean(true) instanceof Boolean); // true
console.log(new Number(0) instanceof Number);      // true
console.log(new String("") instanceof String);     // true
両方typeofを見ることができinstanceof、値がブール値、数値、または文字列かどうかをテストするには不十分であるtypeofため、プリミティブブール値、数値、および文字列に対してのみ機能します。またinstanceof、プリミティブなブール値、数値、文字列では機能しません。
幸い、この問題には簡単な解決策があります。のデフォルト実装toString(つまり、でネイティブに定義されているObject.prototype.toString)は、[[Class]]プリミティブ値とオブジェクトの両方の内部プロパティを返します。
function classOf(value) {
    return Object.prototype.toString.call(value);
}
console.log(classOf(true));              // [object Boolean]
console.log(classOf(0));                 // [object Number]
console.log(classOf(""));                // [object String]
console.log(classOf(new Boolean(true))); // [object Boolean]
console.log(classOf(new Number(0)));     // [object Number]
console.log(classOf(new String("")));    // [object String]
[[Class]]値の内部プロパティは、値よりもはるかに便利ですtypeof。を使用Object.prototype.toStringして、typeof次のように独自の(より便利な)演算子のバージョンを作成できます。
function typeOf(value) {
    return Object.prototype.toString.call(value).slice(8, -1);
}
console.log(typeOf(true));              // Boolean
console.log(typeOf(0));                 // Number
console.log(typeOf(""));                // String
console.log(typeOf(new Boolean(true))); // Boolean
console.log(typeOf(new Number(0)));     // Number
console.log(typeOf(new String("")));    // String
この記事がお役に立てば幸いです。プリミティブとラップされたオブジェクトの違いについて詳しく知るには、次のブログ投稿を読んでください。JavaScriptプリミティブの秘密の生活