How much does typeof know in JavaScript?

Source: Internet
Author: User

The typeof operator describes:
TypeOf is a unary operation that can be any type before it is placed in an operand.
The return value is a string that describes the type of the operand.

Do you know the result of the typeof operation below?

typeof (1);
typeof (NaN);
typeof (Number.min_value);
typeof (Infinity);
typeof ("123");
typeof (True);
typeof (Window);
typeof (document);
typeof (NULL);
typeof (Eval);
typeof (Date);
typeof (SSS);
typeof (undefined);

How many do you have?

If you see later, not very clear, please look at the following (understand people do not have to look down):
typeof is a unary operator that returns a result that is always a string that returns different results for different operands.
The specific rules are as follows:
One, for operands of numeric types, the value returned by TypeOf is number. For example: typeof (1), the value returned is number.
The above is the general number, and for the unconventional numeric type, the result returns the number. such as typeof (Nan), Nan in
JavaScript represents a special non-numeric value, although it is itself a numeric type.
In JavaScript, there are several special types of numbers:
Infinity denotes Infinity Special value
NaN Special non-numeric value
Number.MAX_VALUE The maximum number that can be represented
Number.min_value minimum number to be represented (closest to 0)
Number.NaN Special non-numeric values
Number.POSITIVE_INFINITY represents a special value for positive infinity
Number.negative_infinity represents a special value for negative infinity

The above special type, in the operation with TypeOf, the result will be number.

Second, for string types, the value returned by TypeOf is String. For example, the value returned by typeof ("123") is a string.
For Boolean types, the value returned by TypeOf is Boolean. For example, the value returned by typeof (True) is Boolean.
The value returned for the object, array, and null is object. For example, typeof (window), typeof (document), typeof (NULL) returns a value of object.
V. For a function type, the value returned is function. For example: typeof (eval), the value returned by typeof (Date) is function.
VI. If the operand is undefined (for example, a nonexistent variable, function, or undefined), the undefined will be returned. For example: typeof (SSS), typeof (undefined) return undefined.

After reading the six rules, and then look back, it is not very simple ...

Below we will verify with the program code:

<script>
document.write ("typeof (1):"+typeof(1) +"<br>");
document.write ("typeof (NaN):"+typeof(NaN) +"<br>");
document.write ("typeof (Number.min_value):"+typeof(Number.min_value) +"<br>")
document.write ("typeof (Infinity):"+typeof(Infinity) +"<br>")
document.write ("typeof (\" 123\ "):"+typeof("123")+"<br>")
document.write ("typeof (True):"+typeof(true)+"<br>")
document.write ("typeof (Window):"+typeof(window) +"<br>")
document.write ("typeof (document):"+typeof(document) +"<br>")
document.write ("typeof (NULL):"+typeof(NULL)+"<br>")
document.write ("typeof (eval):"+typeof(eval) +"<br>")
document.write ("typeof (Date):"+typeof(Date) +"<br>")
document.write ("typeof (SSS):"+typeof(SSS) +"<br>")
document.write ("typeof (undefined):"+typeof(undefined) +"<br>")
</script>

Eg:

if (typeof checked = = "undefined") {
vectoraction = 0;
Isview = "hidden";
}else{
Vectoraction = 1;
Isview = "Display";
}

How much does typeof know in JavaScript?

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.