Certainly a lot of small partners see the topic will be a Leng, typeof is what, right JavaScript typeof in fact rarely used, so a lot of small partners are not very clear, but we still have some understanding of this, Then we will take you today to introduce in detail the use of typeof in JavaScript!
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>");d Ocument.write ("typeof (Nan):" +typeof (nan) + " <br> ");d Ocument.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>
Summarize:
I believe that through the detailed study of this article, the small partners of JavaScript in the use of TypeOf have a better understanding of your work to help!
Related recommendations:
An example analysis of typeof and type judgment in JavaScript
Summarize the usage of typeof in JS
Seven types of value and typeof operators in JavaScript
The arithmetic rules of Js typeof