Four Methods of JavaScript to Judge Data Type

  • 2021-08-16 23:01:37
  • OfStack

This paper provides four methods to judge js data type, and records the differences between them, namely typeof operator, instanceof operator, constructor attribute, Object. prototype. toString method.

1. Use typeof to determine the data type


console.log(' Test  Number ->', typeof 1); // number
console.log(' Test  Boolean ->', typeof true); // boolean
console.log(' Test  String ->', typeof ''); // string
console.log(' Test  null ->', typeof null); // object
console.log(' Test  undefined ->', typeof undefined); // undefined
console.log(' Test  NaN ->', typeof NaN); // number
console.log(' Test  function ->', typeof function () { }); // function
console.log(' Test  Object ->', typeof {}); // object
console.log(' Test  Array ->', typeof []); // object
console.log(' Test  Date ->', typeof new Date()); // object
console.log(' Test  Error ->', typeof new Error()); // object
console.log(' Test  RegExp ->', typeof new RegExp()); // object
console.log(' Test  Symbol ->', typeof Symbol()); // symbol
console.log(' Test  Map ->', typeof new Map()); // object
console.log(' Test  Set ->', typeof new Set()); // object

The console output is as follows:

Test Number- > number
Test Boolean- > boolean
Test String- > string
Test null- > object
Test undefined- > undefined
Test NaN- > number
Test function- > function
Test Object- > object
Test Array- > object
Test Date- > object
Test Error- > object
Test RegExp- > object
Test Symbol- > symbol
Test Map- > object
Test Set- > object

Summary:

1. typeof can only judge:

String (returns string), Number (returns number), Boolean (returns boolean), undefined (returns undefined), function (returns function), Symbol (Returns symbol)

2. For new, all constructs return object

3. object is returned for both Object and Array

2. Use instanceof to determine the data type


console.log(' Test  Number ->', 1 instanceof Number); // false
console.log(' Test  Boolean ->', true instanceof Boolean); // false
console.log(' Test  String ->', '' instanceof String); // false
// console.log(' Test  null ->', null instanceof null); // TypeError: Cannot read property 'constructor' of null
// console.log(' Test  undefined ->', undefined instanceof undefined); // TypeError: Cannot read property 'constructor' of undefined
console.log(' Test  NaN ->', NaN instanceof Number); // false
console.log(' Test  function ->', function () { } instanceof Function); // true
console.log(' Test  Object ->', {} instanceof Object); // true
console.log(' Test  Array ->', [] instanceof Array); // true
console.log(' Test  Date ->', new Date() instanceof Date); // true
console.log(' Test  Error ->', new Error() instanceof Error); // true
console.log(' Test  RegExp ->', new RegExp() instanceof RegExp); // true
console.log(' Test  Symbol ->', Symbol() instanceof Symbol); // false
console.log(' Test  Map ->', new Map() instanceof Map); // true
console.log(' Test  Set ->', new Set() instanceof Set); // true

console.log(' Test  new Number ->', new Number(1) instanceof Number); // true
console.log(' Test  new Boolean ->', new Boolean(true) instanceof Boolean); // true
console.log(' Test  new String ->', new String('') instanceof String); // true

The console output is as follows:

Test Number- > false
Test Boolean- > false
Test String- > false
Test NaN- > false
Test function- > true
Test Object- > true
Test Array- > true
Test Date- > true
Test Error- > true
Test RegExp- > true
Test Symbol- > false
Test Map- > true
Test Set- > true
Test new Number- > true
Test new Boolean- > true
Test new String- > true

Summary:

1. null and undefined cannot be judged

2. The basic data types Number, String and Boolean cannot be judged

3. instanceof is used to judge whether an object is an instance of a 1 data type.
In the above example 1, true, ''is not an instance, so it is judged as false

3. Use constructor to determine the data type


console.log(' Test  Number ->', (1).constructor === Number); // true
console.log(' Test  Boolean ->', true.constructor === Boolean); // true
console.log(' Test  String ->', ''.constructor === String); // true
// console.log(' Test  null ->', null.constructor === null); // TypeError: Cannot read property 'constructor' of null
// console.log(' Test  undefined ->', undefined.constructor); // TypeError: Cannot read property 'constructor' of undefined
console.log(' Test  NaN ->', NaN.constructor === Number); // true  Attention :NaN And infinity1 Sample is Number Type of 1 Special values 
console.log(' Test  function ->', function () { }.constructor === Function); // true
console.log(' Test  Object ->', {}.constructor === Object); // true
console.log(' Test  Array ->', [].constructor === Array); // true
console.log(' Test  Date ->', new Date().constructor === Date); // true
console.log(' Test  Error ->', new Error().constructor === Error); // true
console.log(' Test  RegExp ->', new RegExp().constructor === RegExp); // true
console.log(' Test  Symbol ->', Symbol().constructor === Symbol); // true
console.log(' Test  Map ->', new Map().constructor === Map); // true
console.log(' Test  Set ->', new Set().constructor === Set); // true

The console output is as follows:

Test Number- > true
Test Boolean- > true
Test String- > true
Test NaN- > true
Test function- > true
Test Object- > true
Test Array- > true
Test Date- > true
Test Error- > true
Test RegExp- > true
Test Symbol- > true
Test Map- > true
Test Set- > true

Summary:

Can not judge null, undefined, other can

4. Use Object. prototype. toString to determine the data type


console.log(' Test  Number ->', Object.prototype.toString.call(1)); // [object Number]
console.log(' Test  Boolean ->', Object.prototype.toString.call(true)); // [object Boolean]
console.log(' Test  String ->', Object.prototype.toString.call('')); // [object String]
console.log(' Test  null ->', Object.prototype.toString.call(null)); // [object Null]
console.log(' Test  undefined ->', Object.prototype.toString.call(undefined)); // [object Undefined]
console.log(' Test  NaN ->', Object.prototype.toString.call(NaN)); // [object Number]
console.log(' Test  function ->', Object.prototype.toString.call(function () { })); // [object Function]
console.log(' Test  Object ->', Object.prototype.toString.call({})); // [object Object]
console.log(' Test  Array ->', Object.prototype.toString.call([])); // [object Array]
console.log(' Test  Date ->', Object.prototype.toString.call(new Date())); // [object Date]
console.log(' Test  Error ->', Object.prototype.toString.call(new Error())); // [object Error]
console.log(' Test  RegExp ->', Object.prototype.toString.call(new RegExp())); // [object RegExp]
console.log(' Test  Symbol ->', Object.prototype.toString.call(Symbol())); // [object Symbol]
console.log(' Test  Map ->', Object.prototype.toString.call(new Map())); // [object Map]
console.log(' Test  Set ->', Object.prototype.toString.call(new Set())); // [object Set]

The console output is as follows:

Test Number- > [object Number]
Test Boolean- > [object Boolean]
Test String- > [object String]
Test null- > [object Null]
Test undefined- > [object Undefined]
Test NaN- > [object Number]
Test function- > [object Function]
Test Object- > [object Object]
Test Array- > [object Array]
Test Date- > [object Date]
Test Error- > [object Error]
Test RegExp- > [object RegExp]
Test Symbol- > [object Symbol]
Test Map- > [object Map]
Test Set- > [object Set]

Summary:

The most perfect method to judge the data type at present

Conclusion: The above is the author's test and summary. If there are any mistakes or incompleteness, please correct them.

Above is JavaScript judgment data type 4 methods of the detailed content, more about JavaScript judgment data type information please pay attention to this site other related articles!


Related articles: