VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • JavaScript文章

    • 33 Extremely Useful JavaScript One-Liners
    • How the new Operator Works
    • ES5 Object-Oriented Programming
    • ES6 Object-Oriented Programming
    • Performance Comparison of Multiple Array Deduplication Methods
    • JS Randomly Shuffle an Array
    • Detect Whether the Browser Is on a Mobile Device
    • Convert a 1D Array into a 2D Array by Specified Length
    • Debounce and Throttle Functions
    • JS Get and Modify URL Parameters
    • More Accurate Type Checking Than the typeof Operator
    • 三级目录

  • 学习笔记

  • 前端
  • JavaScript文章
xugaoyi
2020-04-13

More Accurate Type Checking Than the typeof Operator

# More Accurate Type Checking Than the typeof Operator

The return values of Object.prototype.toString for different data types are as follows:

  • Number: returns [object Number].
  • String: returns [object String].
  • Boolean: returns [object Boolean].
  • undefined: returns [object Undefined].
  • null: returns [object Null].
  • Array: returns [object Array].
  • arguments object: returns [object Arguments].
  • Function: returns [object Function].
  • Error object: returns [object Error].
  • Date object: returns [object Date].
  • RegExp object: returns [object RegExp].
  • Other objects: returns [object Object].

This means that Object.prototype.toString can reveal the actual type of a value.

Object.prototype.toString.call(2) // "[object Number]"
Object.prototype.toString.call('') // "[object String]"
Object.prototype.toString.call(true) // "[object Boolean]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(Math) // "[object Math]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call([]) // "[object Array]"
1
2
3
4
5
6
7
8

Using this characteristic, you can write a type-checking function that is more accurate than the typeof operator.

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"
1
2
3
4
5
6
7
8
9
10
11

Building on this type function, you can also add methods that check for specific data types.

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

['Null',
 'Undefined',
 'Object',
 'Array',
 'String',
 'Number',
 'Boolean',
 'Function',
 'RegExp'
].forEach(function (t) {
  type['is' + t] = function (o) {
    return type(o) === t.toLowerCase();
  };
});

type.isObject({}) // true
type.isNumber(NaN) // true
type.isRegExp(/abc/) // true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36
JS Get and Modify URL Parameters
Fourth-Level File (Test)

← JS Get and Modify URL Parameters Fourth-Level File (Test)→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode