Introduction to the use of typeof in JavaScript

黄舟
Release: 2018-05-30 11:42:08
Original
2700 people have browsed it

I am sure many friends will be stunned when they see the question. What is typeof? That’s right Typeof in JavaScript is rarely used, so many friends are not very clear about it, but we still think about it If you have a certain understanding, then today we will give you a detailed introduction to the use of typeof in JavaScript!

typeofOperatorIntroduction:
typeof is a unary operation, placed before an operand, and the operand can be of any type.
The return value is a string, which describes 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);
Copy after login

Look, how many do you know?
If you don’t understand it clearly after reading it, please read below (those who understand don’t need to read further):
typeof is a unary operator, and the result it returns is always a string. operands, it returns different results.
The specific rules are as follows:
1. For numeric type operands, the value returned by typeof is number. For example: typeof(1), the returned value is number.
The above are regular numbers. For unconventional number types, the result returned is also number. For example, typeof(NaN), NaN represents a special non-numeric value in
JavaScript, although it itself is a numeric type.
In JavaScript, there are several special numeric types:
Infinity represents infinity special value
NaN                                                          」                      cco wealth number `
Number.MIN_VALUE The smallest representable number (closest to zero)
Number.NaN                                                                                                                                                                                                                                                                       ler】 For special types, when using typeof to perform operations, the result will be number.
2. For string type, the value returned by typeof is
string
. For example, the value returned by typeof("123") is string.
3. For
Boolean type, the value returned by typeof is boolean. For example, the value returned by typeof(true) is boolean. 4. For objects, arrays, and null, the returned value 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: the values ​​returned by typeof(eval) and typeof(Date) are functions. 6. If the operand is not defined (such as a non-existent variable, function or undefined), undefined will be returned. For example: typeof(sss), typeof(undefined) both return undefined. After reading the six rules, look back and see if it is very simple...
Let’s verify it 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>
Copy after login



Summary:

I believe that through detailed study of this article, friends will have an understanding of the use of typeof in JavaScript. I hope it will be helpful to your work!

Related recommendations:

Example analysis of typeof and type judgment in JavaScript

Summary of the usage of typeof in js

Seven value types and typeof operators in JavaScript

Js typeof operation rules

The above is the detailed content of Introduction to the use of typeof in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template