Javascripttypeof usage and typeof operator Introduction [Details] _ javascript skills

Source: Internet
Author: User
The following is a detailed introduction to the typeof operator and some usage, analysis, and learning of typeof operators. After reading this article, we should be able to gain some benefits. The following is a detailed introduction to the typeof operator and some usage, analysis, and learning of typeof operators. After reading this article, we should be able to gain some benefits.

Arrays are often used in js. For example, if multiple inputs with the same name are generated dynamically, You need to determine whether the input is an array during submission.
If (document. mylist. length! = "Undefined") {} This usage is incorrect.
Correct is if (typeof (document. mylist. length )! = "Undefined "){}
Or if (! IsNaN (document. mylist. length )){}
The number of typeof operations is undefined, and "undefined" is returned ".
Number of operations: number typeof (x) = "number"
String typeof (x) = "string"
Boolean value typeof (x) = "boolean"
Object, array, and null typeof (x) = "object"
Function typeof (x) = "function"
The typeof operator returns a string that represents the Data Type of the expression.
Possible strings include: "number", "string", "boolean", "object", "function", and "undefined ".
For example:
Alert (typeof (123); // typeof (123) returns "number"
Alert (typeof ("123"); // typeof ("123") returns "string"
Typeof Operator
Returns a string that represents the expression's data type.
Typeof [() expression [];
The expression parameter is an arbitrary expression that needs to be searched for type information.
Description
The typeof operator returns the type information as a string. There are six possible types of typeof return values: "number," "string," "boolean," "object," "function," and "undefined ."
Parentheses in the typeof syntax are optional.

Introduction to the typeof OPERATOR:
Typeof is a one-dimensional operation. Before a single operation, the operation can be of any type.
The return value is a string that represents the type of the number of operations.
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 );
What are you going to do?

If you do not quite understand it after reading it, see the following (people who understand it do not need to read it ):
Typeof is a unary operator. It returns a string and returns different results for different operands.
The specific rules are as follows:
1. For numeric operands, the value returned by typeof is number. For example, for typeof (1), the returned value is number.
The preceding figure shows the regular number. For an unconventional number type, the result returned is also number. For example, typeof (NaN), NaN in
JavaScript represents a special non-numeric value, although it is a numerical type.
In JavaScript, there are several special numeric types:
Infinity indicates a special value of Infinity.
NaN special non-numeric value
Number. MAX_VALUE indicates the maximum Number.
Number. MIN_VALUE represents the smallest Number (closest to zero)
Number. NaN special non-numeric value
Number. POSITIVE_INFINITY indicates the special value of positive infinity.
Number. NEGATIVE_INFINITY indicates the special value of negative infinity.
For the above special types, when typeof is used for calculation, the result will be number.
2. For the 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 values are objects. 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) and typeof (Date) return all function values.
6. If the number of operations is not defined (for example, a non-existent variable, function, or undefined), undefined is returned. For example, typeof (sss) and typeof (undefined) both return undefined.

Script
Document. write ("typeof (1):" + typeof (1) +"
");
Document. write ("typeof (NaN):" + typeof (NaN) +"
");
Document. write ("typeof (Number. MIN_VALUE):" + typeof (Number. MIN_VALUE) +"
")
Document. write ("typeof (Infinity):" + typeof (Infinity) +"
")
Document. write ("typeof (\" 123 \ "):" + typeof ("123") +"
")
Document. write ("typeof (true):" + typeof (true) +"
")
Document. write ("typeof (window):" + typeof (window) +"
")
Document. write ("typeof (document):" + typeof (document) +"
")
Document. write ("typeof (null):" + typeof (null) +"
")
Document. write ("typeof (eval):" + typeof (eval) +"
")
Document. write ("typeof (Date):" + typeof (Date) +"
")
Document. write ("typeof (sss):" + typeof (sss) +"
")
Document. write ("typeof (undefined):" + typeof (undefined) +"
")
Script

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.