Tips for using typeof in Javascript _ javascript

Source: Internet
Author: User
The typeof in JavaScript is actually very complicated. It has six types of returned data, which can be used to do many things, but it also has a lot of weird performances. this article lists its usage. For more information, see. Arrays are 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. if (document. mylist. length! = "Undefined") {} this usage is incorrect.
The correct () is if (typeof (document. mylist. length )! = "Undefined "){}
Or if (! IsNaN (document. mylist. length )){}

The number of typeof () operations is undefined, and the return value is "undefined ".
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 an 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"

The typeof operator returns a string that represents the data type of an expression.

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.

The above is all the content of this article. I hope you will like it.

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.