1, the typeof
The typeof operator returns a string representing the typeof the unevaluated operand.
The sample
// Value typeof 37 ==='number'; Typeof = = = 3.14'number';
typeof(42) === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Although it is"Not-A-Number"Abbreviation typeof Number(1) ==='number'; // Number attempts to parse the argument to a numeric typeof 42n ==='bigint'; // String typeof' '= = ='string';
typeof 'bla'= = ='string';
typeof `template literal` === 'string';
typeof '1'= = ='string'; // Note that numeric strings are still the string typeof (typeof 1) ==='string'; // Typeof always returns a String typeof String(1) ==='string'; // String converts any value to a String, which is safer than toString // Boolean typeoftrue= = ='boolean';
typeof false= = ='boolean';
typeof Boolean(1) === 'boolean'; // Boolean() converts arguments based on whether they are true or imaginary typeof!! (1) = = ='boolean'; // Two calls! The (logical non) operator is equivalent to Boolean() // Symbols Typeof Symbol() ==='symbol';
typeof Symbol('foo') = = ='symbol';
typeof Symbol.iterator === 'symbol';
// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined'; Typeof {a: 1} ==='object'; / / using Array. IsArray or Object. The prototype. ToString. Call / / distinguish between Array and ordinary objects typeof = = = [1, 2, 4]'object';
typeof new Date() === 'object';
typeof /regex/ === 'object'; // See the regular expressions section for historical results // The following examples are confusing, dangerous, and useless. Avoid them. typeof new Boolean(true) = = ='object';
typeof new Number(1) === 'object';
typeof new String('abc') = = ='object'; / / function typeoffunction() = = = {}'function';
typeof class C {} === 'function'
typeof Math.sin === 'function';Copy the code
The following table summarizes possible typeof return values.
Undefined | "undefined" |
Null | "object" (seebelow) |
Boolean | "boolean" |
Number | "number" |
BigInt | "bigint" |
String | "string" |
Symbol(New in ECMAScript 2015) | "symbol" |
Host object (provided by the JS environment) |
It depends on the implementation
|
FunctionObject (implemented according to the ECMA-262 specification [[Call]]) | "function" |
Any other object | "object" |
typeof
For base types (
Undefined.
Null.
Boolean.
Number.
String.
BigInt.
Symbol
) in addition to
null
The correct type can be displayed for all objects, including arrays and objects
object
Is displayed for functions
function
. right
Array, object, timestamp, null, etc
object
type
, cannot accurately locate its type.
For arrays, objects, timestamps, we can use instance of to determine
2, instanceof
The instanceof operator is used to check whether the constructor’s prototype property is present in the prototype chain of an instance object. It works by looking up the prototype chain.
For example:
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda'.'Accord', 1998);
console.log(auto instanceof Car);
// expected output: true
console.log(auto instanceof Object);
// expected output: trueCopy the code
The 👆 example shows that the constructor Car’s prototype property is on the prototype chain of the instance object Auto
Note: Instanceof can determine the data type, but its type is based on the Object.
Is there a better way to determine the type of variable?
Some can use Object. The prototype. ToString. Call (var), can more accurately judge the type of a variable.
3, the Object. The prototype. ToString. Call
Object. The prototype. ToString. Call () is in the Object. The prototype. The toString () method to implement the above, make its can determine a variety of data types
Each object has a toString() method, which is called automatically when the object is represented as a text value, or when an object is referenced as an expected string. By default, the toString() method is inherited by each Object. If this method is not overridden in a custom object, toString() returns “[Object type]”, where type is the type of the object.
There are some other interesting things about toString, and this is a good article. Let’s learn together
Juejin. Cn/post / 684490…