This is the 29th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

  • Typeof operator, instanceof operator

The types of values in JS are divided into primitive value types and object types.

Primitive value types include number, String, Boolean, null, and undefined; The object type is Object.

The primitive value type is not an object in the first place. Also, note the difference between ‘hello’, which is a String literal and a primitive type, and new String(‘hello’), which is an object. The typeof operator returns a completely different value:

typeof 'hello';  // 'string'
typeof new String('hello');  // 'object'
Copy the code

The reason why so many people confuse String literals with String objects is that JS syntax is too indulgent for you.

When ‘hello’. Length is executed and returns 5 as expected, you assume that ‘hello’ is a String, otherwise it wouldn’t have a String property. This syntax is called “boxing”. This syntax is called “boxing”. Do not think that JS helps you box, so you can write code without breaking the box.

1.Operator (1)typeofOperator format: resul t=typeofVariable return value:undefinedValue Undefined Boolean Boolean value string String number Numerical value object,null

functionFunction (2)instanceofOperator format:result = variable instanceof constructorThe return value:true

false
Copy the code
  • Standard box model and IE box model

W3Cschool’s illustration of the box model

Js has no concept of function overloading. In other languages (such as Java), functions with the same name can exist, as long as the number or type of arguments passed is different.

In JS, after defining two functions with the same name, the latter function overrides the previous one.

In combination with this problem, the function declaration is advanced because the function declaration is promoted. Since there is a function with the same name, subsequent add overrides the first add function, so both calls to add() return the same value. So y and z are both equal to 4.