Types in javascript

  node.js, question

1. On page 23 of the third edition of JavaScript Advanced Programming, the author has a very clear description:

There are five simple data types (undefined, null, boolean, number, string) and one complex type (Object) in ECMAScript

2. InJavaScript secret gardenPart of the object, also has a very clear description:

All variables in JavaScript are objects, with two exceptions null and undefined

So my question is:

1.var bol = true; var num = 1; var str = 'abc'Of whichbol,num,strIs it an object or a basic type?

2. A basic type is declared in 2.JavaScript. Has the constructor of the corresponding wrapper type been called?

The following is the test I did. Great God can ignore it:

var num = 1;
 var numObj = new Number(1);
 console.log(num instanceof Number);  // false
 console.log(num.constructor);  // Number
 console.log(numObj instanceof Number);  // true
 console.log(numObj.constructor);  // Number
 var bol = true;
 var bolObj = new Boolean(true);
 console.log(bol instanceof Boolean);  // false
 console.log(bol.constructor);  // Boolean
 console.log(bolObj instanceof Boolean);  // true
 console.log(bolObj.constructor);  // Boolean
 var str = "abc";
 var strObj = new String("abc");
 console.log(str instanceof String);  // false
 console.log(str.constructor);  // String
 console.log(strObj instanceof String);  // true
 console.log(strObj.constructor);  // String

There is a lot of doubt in the test: the values of the basic types are not examples of the corresponding packaging types, butconstructorHowever, it points to the corresponding packaging type. .

“Secret” inside’s statement that all variables are objects is questionable.
Simple primitive types (string, boolean, number, null, undefined) are not objects themselves

var num = 1;
 typeof a // "number"

Num is the base type number, not object.

As can be seen from your code below

console.log(num instanceof Number);  // false

So the first question is

fundamental type

But why

console.log(num.constructor); // Number

In fact, the Number constructor is called by default behind num to construct a Number wrapper type (this wrapper type is actually very similar to that of Java, except that JS wrapper type is completed by calling the constructor Number), thus making num an object, which is the case above

Second question

No, but the constructor is called only when it is used as an object (calling properties, methods). your code num.constructor returns Number, which clearly illustrates this problem.