Home  >  Article  >  Web Front-end  >  Introduction to the use of typeof in JavaScript

Introduction to the use of typeof in JavaScript

黄舟
黄舟Original
2018-05-30 11:42:082319browse

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);

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:




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!

Statement:
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