最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - typeOf is not defined - Stack Overflow

programmeradmin1浏览0评论

I normally use typeOf() function instead of typeof operator to check the data type, however, I came across two problems today:

  1. typeOf() and typeof are giving me a different result.

  1. even worse, after I updated my Chrome browser, I am getting 'typeOf is not defined' message.

So my question is:

  1. What is the difference between typeOf() and typeof?

  2. Why am i getting 'typeOf is not defined' after updating Chrome? Is typeOf() obsolete?

I normally use typeOf() function instead of typeof operator to check the data type, however, I came across two problems today:

  1. typeOf() and typeof are giving me a different result.

  1. even worse, after I updated my Chrome browser, I am getting 'typeOf is not defined' message.

So my question is:

  1. What is the difference between typeOf() and typeof?

  2. Why am i getting 'typeOf is not defined' after updating Chrome? Is typeOf() obsolete?

Share Improve this question asked Sep 12, 2019 at 17:00 MikeMike 3914 silver badges15 bronze badges 2
  • 1 "What is the difference between typeOf() and typeof?" the one with the capital letter does not exist by default – VLAZ Commented Sep 12, 2019 at 17:02
  • 1 Also typeof 1 + "2" will be resolves as (typeof 1) + "2" due to precedence rules – VLAZ Commented Sep 12, 2019 at 17:03
Add a ment  | 

2 Answers 2

Reset to default 6

There is no typeOf function, there exist only typeof operator - https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/typeof

typeof 1 + "2" is getting evaluated as ->

(typeof 1) + "2"

"number" + "2"

"number2" <-- Concatenation

I think there is no typeOf function, only the type of operator. As you can see, your code in vanilla javascript is throwing errors to the console:

typeOf("1");
typeOf(false);

Also, 1 + "1" will be concat was a string ("11"). So, by doing typeof (1+"1") you are doing typeof("11") which is indeed a string.

Ultimately, you could implement your own function like this, to achieve the result that you want:

let typeOf = (type) => console.log(typeof (type)); 
typeOf(1 + "1");

发布评论

评论列表(0)

  1. 暂无评论