Use of JavaScript typeof () and operator usage

Source: Internet
Author: User
Tags arrays eval numeric numeric value

typeof operator
Returns a string that is used to represent the data type of an expression.

typeof[() expression[]];

The expression parameter is any expression that needs to find the type information.

Description
The typeof operator returns the type information as a string. There are six possible typeof return values: "Number," "String," "Boolean," "Object," "function," and "undefined."

Parentheses in the typeof syntax are optional

<title>debug example</title>
<script type= "text/web Effects" >

function Afunction (INUM1) {
if (typeof iNum1 = = "Number") {
Alert ("number");
}

if (typeof iNum1 = = "string") {
Alert ("string");
}
}
Afunction ("a");
Afunction (1);
</script>
<body>
</body>


In JS used in the array, such as multiple names of the same input, if dynamically generated, submitted to determine whether it 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 typeof operand is undefined, and the return is "undefined".

The number of operands is typeof (x) = "Number"

String typeof (X) = "string"

Boolean value typeof (X) = "Boolean"

objects, arrays, and Null typeof (X) = "Object"

Functions typeof (X) = "function


uses an array in JS, such as multiple names (the same) input, and if it is dynamically generated, it is necessary to determine whether it is an array when submitted. if (document.mylist.length!= "undefined") {} This usage is incorrect.
Correct () is if (typeof (Document.mylist.length)!= "undefined") {}
or if (!isnan (Document.mylist.length)) {}
Typ EOF (the) is not defined, the return (of) is "undefined".
operands are numeric typeof (x) = "number"
String typeof (X) = "string"
Boolean typeof (X) = "Boolean"
object, array and null typeof (X) = The "Object"
Function typeof (x) = "function"
typeof operator returns a String that represents the data type () of an expression. The
Possible () strings are: "Number", "string", "Boolean", "Object", "function", and "undefined".
such as:

Alert (typeof (123));//typeof (123) returns "Number"
Alert (typeof ("123"));//typeof ("123") returns "string"
<title>debug example</title>
<script type= "Text/javascript" >

function Afunction (INUM1) {
if (typeof iNum1 = = "Number") {
Alert ("number");
}

if (typeof iNum1 = = "string") {
Alert ("string");
}
}
Afunction ("a");
Afunction (1);
</script>
<body>
</body>


typeof operator
Returns a string that is used to represent the data type (of) an expression.
typeof[() expression[]];
The expression parameter is any expression that needs to find the type information ().

The typeof operator returns the type information as a string. There are six possible typeof return values: "Number," "String," "Boolean," "Object," "function," and "undefined."

The parentheses in the TypeOf syntax are optional. Introduction to the TypeOf operator:
TypeOf is a unary operation, which can be any type of operation before it is placed in one op-count.
The return value is a string that 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);

Instance

<title>www.111cn.net </title>
<script language= "JavaScript" >
<!--
var x = "Hello", y;
Alert ("Variable x value is" + typeof (X));
Alert ("Variable y value is" + typeof (Y));
Alert ("Variable z value is" + typeof (Z));
-->
</script>
<body>

</body>


How many do you have?

If you look at it later, not very clear, please see below (understand the people do not have to look down):
typeof is a unary operator, which returns a result that is always a string, and for different operands, it returns a different result.
The specific rules are as follows:
For the operand of a numeric type, the value returned by TypeOf is number. For example: typeof (1), the return value is number.
The above is a regular number, and for unconventional numeric types, the result returns a number. such as typeof (Nan), Nan in
JavaScript represents a special Non-numeric value, although it is itself a numeric type.
In JavaScript, there are several special types of numbers:
Infinity represents Infinity Special value
NaN Special non-numeric value
Number.MAX_VALUE The maximum number that can be represented
Number.min_value can represent the smallest number (closest to 0)
Number.NaN Special non-numeric values
Number.POSITIVE_INFINITY a special value representing positive infinity
Number.negative_infinity represents a special value of negative infinity

The above special type, which is typeof, will result in number.

For a string type, the value returned by TypeOf is String. For example, the value returned by typeof ("123") is a string.
For a Boolean type, the value returned by TypeOf is a Boolean. For example, the value returned by typeof (True) is a Boolean.
The values returned for objects, arrays, and Null are object. For example, typeof (window), typeof (document), typeof (NULL) return values are object.
For a function type, the value returned is a function. For example: typeof (eval), the value returned by typeof (Date) is a function.
If the OP count is undefined (for example, a nonexistent variable, function, or undefined), it will return undefined. For example: typeof (SSS), typeof (undefined) are returned to undefined.

Instance

<title>using typeof to determine the type of variables</title>
<script language= "JavaScript1.1" >
<!--
var Bmyvar = true;

var nmyvar = 35;

var Smyvar = "This is a string";

var Umyvar;

-->
</script>
<body>
<script language= "JavaScript1.1" >
<!--
Document.writeln ("Bmyvar =" + typeof (Bmyvar));

Document.writeln ("<br>nmyvar =" + typeof (Nmyvar));

Document.writeln ("<br>smyvar =" + typeof (Smyvar));

Document.writeln ("<br>umyvar =" + typeof (Umyvar));

-->
</script>
</body>

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.