I normally use typeOf() function instead of typeof operator to check the data type, however, I came across two problems today:
- typeOf() and typeof are giving me a different result.
- even worse, after I updated my Chrome browser, I am getting 'typeOf is not defined' message.
So my question is:
What is the difference between typeOf() and typeof?
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:
- typeOf() and typeof are giving me a different result.
- even worse, after I updated my Chrome browser, I am getting 'typeOf is not defined' message.
So my question is:
What is the difference between typeOf() and typeof?
Why am i getting 'typeOf is not defined' after updating Chrome? Is typeOf() obsolete?
- 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
2 Answers
Reset to default 6There 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");