Yesterday to play with colleagues, colleagues gave me a code topic, is about typeof, feel very fun, here to share to everyone, by the way consolidate their own typeof knowledge points of the summary. If there is no, please give points and make progress together.
The code is like this:
So, what do you think the Chrome debugger will print when you run the code?
Please look, the effect chart
Why did this result occur?
First analyze the two null in the above figure
because JavaScript first resolves (hoisting) before executing the code.
What is pre-resolution (hoisting)?
In JavaScript, you can declare multiple var statements at any point in the function, as they do at the top of the function, and give a undefined initial value to the variable that the Var declares. This behavior is called pre-resolution (hoisting).
So, before executing the above code, JavaScript will first parse all of the Var variables (a,b,c) (hoisting) when executing a| | When null, A is undefined, and because it is a | | operation, so the result is null slightly.
c| | Null is a truth.
Again, why would typeof F be undefined? F is not defined, should not be an error?
Yes, if we were to use an undefined variable, the browser would have an error.
However, when you use typeof to determine an undefined variable, you return undefined.
What is the mechanism of the typeof when judging the value type? Summarized as follows:
1. For numeric type values, typeof returns number, noting that Nan is also a numeric type because it represents a special non-numeric value in JavaScript.
2. For string-type values, typeof returns a string.
3. For boolean-type values, typeof returns a Boolean.
4. For objects, arrays, NULL, TypeOf returns object
5, for function type, typeof return function
6. If the OP count is not defined (does not exist or is not assigned), it will return undefined.