This is the 10th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

The purpose of this series of interviews is to learn the relevant knowledge so that you can easily solve various types of questions. This article covers various methods for determining data types in JavaScript.

If you feel helpful, please click 👍 to encourage you

The typeof operator

  • Identify all value types;
  • Identify function types;
  • Recognizes reference types, but cannot distinguish between objects, arrays, andnull.
  • InfinityNaNWill be recognized asnumber, even thoughNaNNot-A-NumberShort for “Not a number”.
  • We can usetypeofTo detect the presence of a variable, such asif(typeof a! ="undefined"){}And when usedif(a)If theaDoes not exist (not declared), an error is reported.
  let a
  const b = null
  const c = 100
  const d = 'warbler'
  const e = true
  const f = Symbol('f')
  const foo = () = > {}
  const arr = []
  const obj = {}
  console.log(typeof a) //=> undefined
  console.log(typeof b) //=> object
  console.log(typeof c) //=> number
  console.log(typeof d) //=> string
  console.log(typeof e) //=> boolean
  console.log(typeof f) //=> symbol
  console.log(typeof foo) //=> function
  console.log(typeof arr) //=> object
  console.log(typeof obj) //=> object
  console.log(typeof Infinity) //=> number
  console.log(typeof NaN) //=> number
Copy the code

Instanceof method

  • Used to detect reference data types, value types are returnedfalse.
  • The left-hand operand is the object whose class is to be detected, and the right-hand operand is the object’s class. Returns if the object on the left is an instance on the righttrue.Otherwise return false.
  • Check allnewAll objects created by the operator are returnedtrue.
  • detectionnullundefinedReturns thefalse.
  const foo = () = >{}const arr = []
  const obj = {}
  const data = new Date(a)const number = new Number(3)
  console.log(foo instanceof Function) //=> true
  console.log(arr instanceof Array) //=> true
  console.log(obj instanceof Object) //=> true
  console.log(data instanceof Object) //=> true
  console.log(number instanceof Object) //=> true
  console.log(null instanceof Object) //=> false
  console.log(undefined instanceof Object) //=> false
Copy the code

The constructor method

  • In addition toundefinednullAll other types can passconstructorAttribute to determine the type.
  const c = 100
  const d = 'warbler'
  const e = true
  const f = Symbol('f')
  const reg = / ^ [a zA - Z] {5, 20} $/
  const foo = () = >{}const arr = []
  const obj = {}
  const date = new Date(a);const error = new Error(a);console.log(c.constructor === Number) //=> true
  console.log(d.constructor === String) //=> true
  console.log(e.constructor === Boolean) //=> true
  console.log(f.constructor === Symbol) //=> true
  console.log(reg.constructor === RegExp) //=> true
  console.log(foo.constructor === Function) //=> true
  console.log(arr.constructor === Array) //=> true
  console.log(obj.constructor === Object) //=> true
  console.log(date.constructor === Date) //=> true
  console.log(error.constructor === Error) //=> true
Copy the code

Object.prototype.toString.call

  • forObject.prototype.toString()Method, returns an object of the form[object XXX]String of.
  • useObject.prototype.toString.callIs the most accurate way to determine the type of a variable.
  • Object.prototype.toString.callSwitch toObject.prototype.toString.applyCan also.
  let a
  const b = null
  const c = 100
  const d = 'warbler'
  const e = true
  const f = Symbol('f')
  const reg = / ^ [a zA - Z] {5, 20} $/
  const foo = () = >{}const arr = []
  const obj = {}
  const date = new Date(a);const error = new Error(a);const args = (function() {
    return arguments; }) ()console.log(Object.prototype.toString.call(a)) //=> [object Undefined]
  console.log(Object.prototype.toString.call(b)) //=> [object Null]
  console.log(Object.prototype.toString.call(c)) //=> [object Number]
  console.log(Object.prototype.toString.call(d)) //=> [object String]
  console.log(Object.prototype.toString.call(e)) //=> [object Boolean]
  console.log(Object.prototype.toString.call(f)) //=> [object Symbol]
  console.log(Object.prototype.toString.call(reg)) //=> [object RegExp]
  console.log(Object.prototype.toString.call(foo)) //=> [object Function]
  console.log(Object.prototype.toString.call(arr)) //=> [object Array]
  console.log(Object.prototype.toString.call(obj)) //=> [object Object]
  console.log(Object.prototype.toString.call(date)) //=> [object Date]
  console.log(Object.prototype.toString.call(error)) //=> [object Error]
  console.log(Object.prototype.toString.call(args)) //=> [object Arguments]
Copy the code

Encapsulate them into simple functions

const getPrototype = (item) = > Object.prototype.toString.call(item).split(' ') [1].replace('] '.' ');
console.log(getPrototype('abc')) //=> String
Copy the code

Null null

We can also use the following method to determine whether a variable is null.

  let exp = null;
  if(! exp &&typeof(exp) ! = ="undefined"&& exp ! = =0) {
    console.log('exp is null');
  }
  if (exp === null) {
    console.log('exp is null');
  }
  if(! exp &&typeof exp === "object") {
    console.log('exp is null');
  }
Copy the code

Undefined undefined

We can also use the following methods to determine whether a variable is undefined.

  let exp;
  if (exp === void 0) {
    console.log('exp is undefined');
  }
Copy the code

digital

We can also use the following method to determine whether a variable is a number.

  let exp = 100;
  If exp is an empty string or a space, isNaN will treat it as a zero
  if (!isNaN(exp)) {
    console.log('exp is number');
  }
  // Use the re to determine whether the string is 0-9
  let reg = / ^ [0-9] +.? [0-9] * /
  if (reg.test(exp)) {
    console.log('exp is number');
  }
  if (parseFloat(exp).toString() ! = ='NaN') {
    console.log('exp is number');
  }
Copy the code

An array of

We can also use the following method to determine whether a variable is an array.

  let exp = [];
  if (Array.isArray(exp)) {
    console.log('exp is Array');
  }
Copy the code