Home > Web Front-end > JS Tutorial > How to determine data type in js

How to determine data type in js

王林
Release: 2020-05-13 09:18:40
forward
3969 people have browsed it

How to determine data type in js

Method 1. JS built-in method typeof

The best choice to detect basic data types is to use typeof

typeof To determine the data type, you can only distinguish between seven basic types, namely "number", "string", "undefined", "boolean", "object", "function", and "symbol" (new in ES6).

For arrays, null, and objects, their relationships are intricate, and using typeof will uniformly return the "object" string.

Example:

var bool = true
var num = 1
var str = 'abc'
var und = undefined
var nul = null
var arr = [1,2,3]
var obj = {}
var fun = function(){}
var reg = new RegExp()

console.log(typeof bool); //boolean
console.log(typeof num); //number
console.log(typeof str); //string
console.log(typeof und); //undefined
console.log(typeof nul); //object
console.log(typeof arr); //object
console.log(typeof obj); //object
console.log(typeof reg); //object
console.log(typeof fun); //function
Copy after login

It can be seen from the results, except that object is returned when detecting null and function is put back when detecting function. For reference types, the return value is object.

Method 2, Object.prototype.toString()

The Object.prototype.toString method returns the type string of the object, so it can be used to determine the type of a value .

var obj = {};
obj.toString() // "[object Object]"上面代码调用空对象的toString方法,结果返回一个字符串object Object,其中第二个Object表示该值的构造函数。这是一个十分有用的判断数据类型的方法。
Copy after login

Object.prototype.toString.call(value)

The above code indicates that the Object.prototype.toString method is called on the value.

The return values ​​of the Object.prototype.toString method of different data types are as follows.

数值:返回[object Number]。
字符串:返回[object String]。
布尔值:返回[object Boolean]。
undefined:返回[object Undefined]。
null:返回[object Null]。
数组:返回[object Array]。
arguments 对象:返回[object Arguments]。
函数:返回[object Function]。
Error 对象:返回[object Error]。
Date 对象:返回[object Date]。
RegExp 对象:返回[object RegExp]。
其他对象:返回[object Object]。
Copy after login

Then using this feature, you can write a more accurate type judgment function than the typeof operator.

Encapsulate a function to determine the type as follows:

var type = function (o){
  var s = Object.prototype.toString.call(o);
  return s.match(/\[object (.*?)\]/)[1].toLowerCase();
};
type({}); // "object"
type([]); // "array"
type(5); // "number"
type(null); // "null"
type(); // "undefined"
type(/abcd/); // "regex"
type(new Date()); // "date"
Copy after login

In addition: You can also add a method to specifically determine a certain type of data

var type = function (o){
  var s = Object.prototype.toString.call(o);
  return s.match(/\[object (.*?)\]/)[1].toLowerCase();
};

var arr = ['Null', 'Undefined', 'Object', 'Array', 'String', 'Number', 
 'Boolean', 'Function', 'RegExp']

arr.forEach(function (t) {
  type['is' + t] = function (o) {
    return type(o) === t.toLowerCase();
  };
});
Copy after login

After that, we can encapsulate the Methods to use when different needs: as follows

type.isObject({}) // true
type.isNumber(NaN) // true
type.isRegExp(/abc/) // true
Copy after login

Recommended tutorial: js introductory tutorial

The above is the detailed content of How to determine data type in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template