Determine the data type (typeof)

  • Original : MDN-typeof

  • Function : The typeofoperator returns a string indicating the type of the unevaluated operand.

  • Method : typeof operandortypeof(operand)

    • operand: An expression representing an object or primitive value, the type of which will be returned.
  • Description :

Here are the typeofpossible return values:

Types of result
Undefined ‘undefined’
Null ‘object’
Boolean ‘boolead’
Number ‘number’
BigInt ‘bigint’
String ‘string’
Symbol ‘symbol’
Function ‘function’
Any other object ‘object’
  • Code :
/**
 * @name typeof测试
 * @description 通过 typeof 检测各个数据类型的返回
 */
const test = {
    
    
  testUndefined: undefined,
  testNull: null,
  testBoolean: true,
  testNumber: 123,
  testBigInt: BigInt(1234), // 大于 2 的 53 次方算 BigInt
  testString: '123',
  testSymbol: Symbol(),
  testFunction: function() {
    
    
    console.log('function');
  },
  testObject: {
    
    
    obj: 'yes',
  },
  testObjectString: new String('String'),
  testObjectNumber: new Number(123),
}

console.log(typeof(test.testUndefined)); // undefined
console.log(typeof(test.testNull));      // object
console.log(typeof(test.testBoolean));   // boolean
console.log(typeof(test.testNumber));    // number
console.log(typeof(test.testBigInt));    // bigint
console.log(typeof(test.testString));    // string
console.log(typeof(test.testSymbol));    // symbol
console.log(typeof(test.testFunction));  // function
console.log(typeof(test.testObject));    // object
console.log(typeof(test.testObjectString));    // object
console.log(typeof(test.testObjectNumber));    // object

Guess you like

Origin blog.csdn.net/weixin_43956521/article/details/111470200