Share a complete wave of JS methods for judging data types
Full of dry goods, a wave of methods that are very common and useful to organize and share with friends in need.
Secretly, Xiaoroubao was not so concise and comprehensive before. The boyfriend of Xiaoroubao helped polish it up again. Now the code looks much simpler.
My other blog shared a more comprehensive way of writing. One method can be used to judge multiple data types. If you are interested, check it out! Method of judging data type in JS
Tips: Explain that the small meat buns here are all arrow functions. The first is the function name, o is the formal parameter, and the return value is a Boolean value. You can also change it to the format you need according to different usage scenarios!
1. Determine whether it is a string (String) type
The code is as follows (example):
isString = o => Object.prototype.toString.call(o) === '[object String]'
Second, determine whether it is a number (Number) type
The code is as follows (example):
isNumber = o => Object.prototype.toString.call(o) === '[object Number]'
Three, determine whether it is an array (Array) type
The code is as follows (example):
isArray= o => Object.prototype.toString.call(o) === '[object Array]'
Four, determine whether it is a non-empty array
The code is as follows (example):
arrNotEmpty=arr => Object.prototype.toString.call(arr).slice(8, -1) === 'Array' && arr.length > 0
Five, determine whether it is an object (Object) type
The code is as follows (example):
isObject= o => Object.prototype.toString.call(o) === '[object Object]'
Six, determine whether it is an object type and non-empty (non-empty object)
The code is as follows (example):
objNotEmpty= o => Object.prototype.toString.call(o) === '[object Object]' && Object.keys(o).length > 0
Seven, determine whether it is a Boolean (Boolean) type
The code is as follows (example):
isBoolean= o => Object.prototype.toString.call(o) === '[object Boolean]'
Eight, determine whether it is a function (Function) type
The code is as follows (example):
isFunction= o => Object.prototype.toString.call(o) === '[object Function]'
Nine, determine whether it is a Null type
The code is as follows (example):
isNull= o => Object.prototype.toString.call(o) === '[object Null]'
10. Determine whether it is undefined (Undefined)
The code is as follows (example):
isUndefined = o => Object.prototype.toString.call(o) === '[object Undefined]'
11. Determine whether it is a date (Date) type
The code is as follows (example):
isDate = o => Object.prototype.toString.call(o) === '[object Date]'
12. Determine whether it is a regular expression
The code is as follows (example):
isRegExp= o => Object.prototype.toString.call(o) === '[object RegExp]'
13. Judge whether it is (Error) type
The code is as follows (example):
isError= o => Object.prototype.toString.call(o) === '[object Error]'
14. Determine whether it is a Symbol type
The code is as follows (example):
isSymbol= o => Object.prototype.toString.call(o) === '[object Date]'
Note: The Symbol type is a newly added data type in ES6. If you want to know more about it, please visit the Symbol type.
15. Determine whether it is a date (Promise) type
The code is as follows (example):
isPromise= o => Object.prototype.toString.call(o) === '[object Promise]'
16. Determine whether it is a Set type
The code is as follows (example):
isSet= o => Object.prototype.toString.call(o) === '[object Set]'
Note: The Set type is a newly added data type in ES6. If you want to know more about it, please visit the Set type
Xiaoroubao has uploaded the JS file for judging the data type, and there are some other methods in it, and you can download the js file and put it in your own project, and then you can call the corresponding method. Download it, attach the download address: data type judgment.js
to sum up
My other blog shared a more comprehensive way of writing. One method can be used to judge multiple data types. If you are interested, check it out! Method of judging data type in JS
The above is the content shared today, some commonly used methods of judging the data type, follow-up will continue to add and improve the mentioned content, as well as other content in the utils toolkit of Xiaoroubao.
Welcome to the personal blog Song Zhang