js-data type judgment method

In JavaScript, there are many ways to determine the data type. Here are some common methods:

Use the typeof operator: typeof is a unary operator in JavaScript that returns the data type of a value. Example:
javascript
Copy
console.log(typeof "hello"); // Output: "string"
console.log(typeof 42); // Output: "number"
console.log(typeof true); // Output: "boolean "
console.log(typeof undefined); // output: "undefined"
console.log(typeof null); // output: "object" (this is a legacy issue)
console.log(typeof [1, 2, 3 ]); // Output: "object"
console.log(typeof { name: "John", age: 30 }); // Output: "object"
console.log(typeof function () {}); // Output : "function"
uses the instanceof operator: The instanceof operator is used to check whether an object is an instance of a particular type. Example:
javascript
Copy
console.log("hello" instanceof String); // output:

console.log(42 instanceof Number); // Output: false
console.log(new Number(42) instanceof Number); // Output: true
console.log(true instanceof Boolean); // Output: false
console.log( new Boolean(true) instanceof Boolean); // Output: true
console.log([1, 2, 3] instanceof Array); // Output: true
console.log({ name: "John", age: 30 } instanceof Object); // Output: true
console.log(function () {} instanceof Function); // Output: true It
should be noted that the instanceof operator can only be used to check the type of the object, for basic data types (such as characters string, number, boolean) is invalid.

Use the Object.prototype.toString method: This is a generic method that returns the concrete type of a value. Example:
javascript
Copy
console.log(Object.prototype.toString.call("hello")); // output: "[object String]"
console.log(Object.prototype.toString.call(42)); // Output: "[object Number]"
console.log(Object.prototype.toString.call(true)); // Output: "[object Boolean]"
console.log(Object.prototype.toString.call(undefined)); // Output: "[object Undefined]"
console.log(Object.prototype.toString.call(null)); // Output: "[object Null]"
console.log(Object.prototype.toString.call([1 , 2, 3])); // Output: "[object Array]"
console.log(Object.prototype.toString.call({ name: "John", age: 30 })); // Output: "[ object Object]"
console.log(Object.prototype.toString.call(function () {})); // Output: "[object Function]"
Using the Array.isArray method: Array.isArray is a static method that checks whether a value for the array. Example:
javascript
Copy
console.log(Array.isArray([1, 2, 3])); // output: true
console.log(Array.isArray({ name: "John", age: 30 })); / / Output: false
console.log(Array.isArray("hello")); // Output: false
Use the combination of typeof and constructor: Sometimes using the typeof operator may get inaccurate results, which can be combined with the constructor attribute judge. Example:
javascript
Copy
console.log(typeof "hello"); // Output: "string"
console.log("hello".constructor === String); // Output: true

console.log(typeof 42); // Output: "number"
console.log((42).constructor === Number); // Output: true

console.log(typeof true); // Output: "boolean"
console.log((true).constructor === Boolean); // Output: true

console.log(typeof undefined); // output: "undefined"
console.log((undefined).constructor === undefined); // output: false

console.log(typeof null); // Output: "object"
console.log((null).constructor === null); // Output: false

console.log(typeof [1, 2, 3]); // Output: "object"
console.log([1, 2, 3].constructor === Array); // Output: true

console.log(typeof { name: "John", age: 30 }); // 输出: "object"
console.log({ name: "John", age: 30 }.constructor === Object); // 输出: true

console.log(typeof function () {}); // output: "function"
console.log((function () {}).constructor === Function); // output: true
Note that the above method is not Completely reliable, because there are some special cases and boundary conditions for data type judgment in JavaScript. When judging the data type, the appropriate method should be selected according to the actual needs, and sufficient testing and verification should be carried out in actual use.

Guess you like

Origin blog.csdn.net/m0_65712362/article/details/132102992