typeof know how much

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.