Is there any difference (compiler/interpreter/juju wise, etc) between the two versions of checking the result of the typeof operator?
I am asking because I see the first version a lot of times, as if it followed a concept, while version two is way more readable and better describes my intention: primarily I am interested in the type of a variable and not a string being equal with something.
UPDATE:
while it’s not part of the original question it worth noting that x == y is never a good practice when you are about to check equality. One should always use the === operator for that.
Update
There is no difference in terms of functionality but it seems that in JavaScript, you get an error in either way (which is good, thanks to JS):
So it seems to be just a habit of developers from other programming languages. For example in PHP if you did:
PHP will silently assign
fooas value to$varbut with following:It will throw an error.
There is no difference in what they do. However first version will throw an error if you happen to write:
Notice
=instead of==or===This is generally good practice, people from other languages have habit of doing it that way in JavaScript also.