'typeOf is not defined

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.

enter image description here

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

enter image description here 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?



Solution 1:[1]

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

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

(typeof 1) + "2"

"number" + "2"

"number2" <-- Concatenation

Solution 2:[2]

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");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2