SoFunction
Updated on 2025-03-01

Detailed explanation of common type judgment methods and differences in JavaScript

typeof operator

typeofOperators can accurately judgenullOther primitive type, it returns a string representing the data type. Here are some examples:

typeof 42 // "number"
typeof "hello" // "string"
typeof true // "boolean"
typeof undefined // "undefined"

It should be noted thattypeof nullreturn"object", This is a historical bug in the JavaScript language itself. Because in memory,typeofIt is to judge the variable's type by judging its binary beginning. The reference type (except function) has three 0s at the beginning, so it returns"object", but it just sonullIts binary representation is all zero, so it is wrongly judged as an object.

instanceof operator

instanceofThe operator is used to determine whether an object is an instance created by a constructor. It can only be used to determine the reference type (because the original type has no prototype), so the original type call will return false. It judges through prototype chain search. The principle is whether the implicit prototype of a is equal to the display prototype of b. Here is an example:

function Person(name) {
   = name;
}

var person1 = new Person('Alice');
person1 instanceof Person; // true

It should be noted that the instance object of the reference type is not only judged as its own reference type, but also is judged as an object, so special attention should be paid when judging the result of the reference type as a selection or judgment condition.

()

()The method returns a string representing an object, which can accurately determine the data type and can handle the original type.

Here are some official descriptions of this method:

  • If this value is not defined, return"[object Undefined]"
  • If this value is null, return"[object Null]"
  • Suppose O is ToObject(this), if the original type passed, it will be calledToObject()Convert primitive types to objects
  • Let class be O[[Class]]Internal properties of
  • Returns a String value, which is to connect three Strings"[object", class and "]"The result of string

[[Class]]Properties are internal properties that each object in JavaScript have, used to specify the type of the object. But we can't use it directly, it usually passes(obj)Method to obtain.

For example:

(123); // "[object Number]"
("hello"); // "[object String]"
(true); // "[object Boolean]"
([]); // "[object Array]"
({}); // "[object Object]"

()

()The method is used to determine whether an object is an array, and it can only be used to judge an array. It is a method introduced by ES5. For example:

([]); // true
({}); // false

Differences and summary

  • typeofCan accurately judgenullprimitive types other than  , but the judgment of reference types is not accurate enough, for exampletypeof []return"object", cannot distinguish between arrays and other objects.
  • instanceofYou can only judge the reference type and judge it by searching the prototype chain, but you cannot accurately judge the original type.
  • ()Being able to accurately judge data types, including primitive types and reference types, is the most accurate method of determining types.
  • ()It is specially used to judge arrays and is the most concise and clear method of determining array types.

Based on the above content, we can conclude that when making type judgments, appropriate methods should be selected according to the specific circumstances to ensure the accuracy and reliability of the judgment. In general, understanding and mastering these types of judgment methods can help us better write JavaScript code and improve the quality and maintainability of the code.

The above is a detailed explanation of the common type judgment methods and differences in JavaScript. For more information about JavaScript type judgment methods, please pay attention to my other related articles!