typeof know how much
Yesterday colleagues showed me a code problem, is about typeof, feel very interesting, here to share to everyone, by the way, and then typeof summary. If not, please give a point and make progress together.
The code is this:
<!DOCTYPE HTML> <Head> <title>typeof</title> <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8"/> </Head> <Body> <Script> vara=a||NULL; varb=C||NULL; varC= typeofF; Console.log (a); Console.log (b); Console.log (c); </Script> </Body></HTML>
So, what do you say when you run the code, what does the Chrome debugger print?
Please look,
Why does this result occur?
Two null in the first analysis
Because JavaScript is pre-parsed (hoisting) before executing the code.
What is pre-parsing (hoisting)?
In JavaScript, you can declare multiple var statements at any point in the function, as if they were declared at the top of the function, and give the variable declared by var a undefined initial value. This behavior we call pre-parsing (hoisting).
So, before executing the above code, JavaScript will first pre-parse all var variables (a,b,c) (hoisting) when executing a| | NULL, A is undefined, and because it is | | operation, so the result is null.
c| | Null is a reason.
Look again, why TypeOf F would 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, the undefined is returned.
What is the mechanism of the typeof when judging the value type? Summarized as follows:
1, for numeric type values, typeof returns number, note: Nan is also a numeric type because it represents a special non-numeric value in JavaScript.
2. For a value of type typeof, the string is returned.
3. For Boolean type values, typeof returns a Boolean.
4. For objects, arrays, and null, typeof returns an object
5, for the function type, typeof returns functions
6. If the operand is not defined (nonexistent or unassigned), the undefined will be returned.
typeof know how much