JavaScript variable type detection

typeof operator

To detect if a variable is not the basic data types, the best tools typeof operator.

typeof determining a variable string, number, Boolean, or undefined when the best tool operator.

If the value of a variable or an object is null, the typeof operator returns Object.

instanceof operator

When the detected value referenced type, instanceof operator may be used.

result = variable instanceof constructor

If the variable is an instance of a given type, then instanceof operator returns true.

person instanceof Object
colors instanceof Array
pattern instanceof RegExp

According to regulations, the value of all reference types are instances of Object. Thus, upon detection of a reference value and Object type constructors, the instanceof operator always returns true. Of course, if using the detection value of the basic instanceof operator type, the operator always returns false, because the object is not a primitive type.

Guess you like

Origin www.cnblogs.com/cherishSmile/p/11102469.html