Relative Content

Tag Archive for javascripttypeof

Need help understanding how prototype propagates in JavaScript

In my code below, I try to easily check “typeof” a given variable, assuming it’s already declared. The native typeof check is not very satisfying when Array returns as “object.” My code mostly works as expected: ‘this is a string’.typeof() yields ‘String’; and Object.prototype.typeof(‘this is a string’) yields ‘String’; but when I tried NaN.typeof(), it yields ‘Number’, but Object.prototype.typeof(NaN) yields ‘NaN’ as expected. Unlikely that this difference would be useful for me in the future, but I just want to understand why the different results. Your insights are very much appreciated. Thank you.