Do you know about TypeOf?

Source: Internet
Author: User

Do you know about TypeOf?

I played with my colleagues yesterday. My colleagues gave me a code question about typeof, which is interesting. I would like to share it with you. By the way, I will consolidate my summary of typeof knowledge points. If any, point out and make progress together.

The Code is as follows:

<!DOCTYPE html>

So what will the chrome debugger print when you run the code?

Please see,


Why does this result appear?

First, two null values in the analysis


Because Javascript will pre-Parse (hoisting) before executing the code ).

What is pre-resolution (hoisting?

In JavaScript, you can declare multiple var statements at any position of the function. They play the same role as the Declaration at the top of the function, the variable declared by var is assigned an undefined initial value. This behavior is called pre-resolution (hoisting ).

Therefore, before executing the above Code, Javascript will first pre-Parse all var variables (a, B, c) (hoisting). When a | null is executed, a is undefined, and because yes | is an operation, the result is null.

C | null is a truth.

Let's take a look at why typeof f is undefined? F is not defined. Shouldn't I report an error?

Yes. If we use an undefined variable, the browser reports an error.

However, when typeof is used to determine an undefined variable, undefined is returned.

What is the mechanism of typeof to determine the value type? Summary:

1. For numeric values, typeof Returns number. Note: NaN is also a numeric value because it represents a special non-numeric value in JavaScript.

2. For string type values, typeof returns string.

3. For boolean values, typeof returns boolean.

4. For objects, arrays, and null, typeof returns the object

5. For function types, typeof returns the function

6. If the number of operations is not defined (does not exist or is not assigned a value), undefined is returned.

Articles you may be interested in:
  • Determine the undefined type using the typeof Method
  • Use typeof to determine whether a function exists in the context
  • Examples of using hasOwnProperty and isPrototypeOf methods in js
  • Differences between instanceof and typeof Operators
  • Summary of the usage of typeof in js

Related Article

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.