The web content discusses the limitations of the typeof operator in JavaScript for type checking and advocates for using Object.prototype.toString.call() as a more reliable alternative, providing examples and a custom type_of function to facilitate its usage.
Abstract
The article "The Best Way to Type Check in Vanilla JS" explains that the typeof operator in JavaScript is insufficient for accurate type checking due to issues like returning "object" for null. The author suggests using Object.prototype.toString.call() instead, as it provides specific object type information without being misled by object-wrapped primitives. The article demonstrates how to create a custom type_of function to streamline this process and discusses the implications of object-wrapped primitives and the use of symbols like Symbol.toStringTag. Despite potential alterations to Object.prototype.toString() behavior, the author concludes that it remains the best method for type checking in JavaScript, while also acknowledging the benefits of stricter type-checking systems like TypeScript and Flow.
Opinions
The author believes that typeof is inadequate for type checking in JavaScript due to its well-known issues, such as returning "object" for null.
Object.prototype.toString.call() is presented as a superior method for type checking, as it correctly identifies the type of any given object, including null.
The article emphasizes the ease of creating a custom type_of function using Object.prototype.toString.call() to simplify the type-checking process.
The author points out that while object-wrapped primitives can cause confusion with typeof, Object.prototype.toString.call() can accurately determine their types.
There is an acknowledgment that Object.prototype.toString() can be unreliable if the Symbol.toStringTag property is defined, but it is still considered the best available option for type checking.
The author suggests that developers should consider using stricter type-checking systems like TypeScript or Flow for better code quality and reliability.
The article concludes with the opinion that despite its imperfections, Object.prototype.toString.call() is the most practical method for type checking in vanilla JavaScript.
The Best Way to Type Check in Vanilla JS
The best way to check the type of a variable in JavaScript is not typeof. Here’s why to use Object.prototype.toString.call() instead and how to alias it to a custom type_of function.
I hope to see type_of in your code instead! Happy coding! 💻👩💻💯🥳
Postscript
Slo Mo pointed out in response to this blog post that it’s possible to change the default behavior of Object.prototype.toString().
Using toString() in this way is unreliable; objects can change the behavior of Object.prototype.toString() by defining a Symbol.toStringTag property, leading to unexpected results.
Basically, instanceof won’t work in iframes, and .constructor can be overwritten directly on any object.
Comparing .constructor to Object.prototype.toString(), I feel that I’m less likely to break Object.prototype.toString() by defining a Symbol.toStringTag property than by overwriting .constructor.