SoFunction
Updated on 2025-04-10

Introduction to the usage of javascript typeof and typeof operator [Details]


Introduction to typeof operator:
typeof is a unary operation. Before an operation, the operation number can be of any type.
It returns a string that specifies the type of the operand.
Do you know the result of the following typeof operation?
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);
See how many you know?

If you don't understand very well after reading it, please read the following (those who understand it don't need to read it down):
typeof is a unary operator, and the result it returns is always a string, and for different operands, it returns different results.
The specific rules are as follows:
1. For operands of numeric types, the value returned by typeof is number. For example: typeof(1), the returned value is number.
The above are regular numbers mentioned, and for unconventional numeric types, the result returns number. For example, typeof(NaN), NaN is
JavaScript represents special non-numeric values, although it is itself a numeric type.
In JavaScript, there are several special number types:
Infinity means infinite special value
NaN's special non-numeric value
Number.MAX_VALUE The maximum number that can be represented
Number.MIN_VALUE The minimum number that can be represented (closest to zero)
Special non-numeric values
Number.POSITIVE_INFINITY Special value indicating positive infinity
Number.NEGATIVE_INFINITY Special value of negative infinity
The above special types are calculated using typeof, and the result will be number.
2. For string types, the value returned by typeof is string. For example, the value returned by typeof("123") is string.
3. For boolean types, the value returned by typeof is boolean. For example, the value returned by typeof(true) is boolean.
4. The value returned for objects, arrays, and null is object. For example, the values ​​returned by typeof(window), typeof(document), and typeof(null) are all objects.
5. For function types, the returned value is function. For example: typeof(eval), typeof(Date) returns all functions.
6. If the operation number is undefined (such as a variable, function, or undefined that does not exist), undefined will be returned. For example: typeof(sss) and typeof(undefined) both return undefined.
<style>
body{font-size:20px;color:#222222;font-family:Stick;line-height:22px;}
</style>
<script>
("typeof(1): "+typeof(1)+"<br>");
("typeof(NaN): "+typeof(NaN)+"<br>");
("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br>")
("typeof(Infinity): "+typeof(Infinity)+"<br>")
("typeof(\"123\"): "+typeof("123")+"<br>")
("typeof(true): "+typeof(true)+"<br>")
("typeof(window): "+typeof(window)+"<br>")
("typeof(document): "+typeof(document)+"<br>")
("typeof(null): "+typeof(null)+"<br>")
("typeof(eval): "+typeof(eval)+"<br>")
("typeof(Date): "+typeof(Date)+"<br>")
("typeof(sss): "+typeof(sss)+"<br>")
("typeof(undefined): "+typeof(undefined)+"<br>")
</script>