Javascript determines the Data Type

Source: Internet
Author: User
1. Determine whether it is an array type 2. determine whether it is a string type
3. Determine whether the data type is Numeric.
4. Determine if it is of the date type
5. determine whether it is a function.
6. Determine whether the object is used

1. Determine whether it is an array type.
Linenum
<SCRIPT type = "text/JavaScript">
// <! [CDATA [
VaR A = [0];
Document. Write (isarray (A), '<br/> ');
Function isarray (OBJ ){
Return (typeof OBJ = 'object') & obj. constructor = array;
}
//]>
</SCRIPT>
2. Determine whether the string type is used.
Linenum
<SCRIPT type = "text/JavaScript">
// <! [CDATA [
Document. Write (isstring ('test'), '<br/> ');
Document. Write (isstring (10), '<br/> ');
Function isstring (STR ){
Return (typeof STR = 'string') & Str. constructor = string;
}
//]>
</SCRIPT>
3. Determine whether the data type is Numeric.
Linenum
<SCRIPT type = "text/JavaScript">
// <! [CDATA [
Document. Write (isnumber ('test'), '<br/> ');
Document. Write (isnumber (10), '<br/> ');
Function isnumber (OBJ ){
Return (typeof OBJ = 'number') & obj. constructor = number;
}
//]>
</SCRIPT>
4. Determine if it is of the date type
Linenum
<SCRIPT type = "text/JavaScript">
// <! [CDATA [
Document. Write (isdate (new date (), '<br/> ');
Document. Write (isdate (10), '<br/> ');
Function isdate (OBJ ){
Return (typeof OBJ = 'object') & obj. constructor = date;
}
//]>
</SCRIPT>
5. determine whether it is a function.
Linenum
<SCRIPT type = "text/JavaScript">
// <! [CDATA [
Document. Write (isfunction (function test () {}), '<br/> ');
Document. Write (isfunction (10), '<br/> ');
Function isfunction (OBJ ){
Return (typeof OBJ = 'function') & obj. constructor = function;
}
//]>
</SCRIPT>
6. Determine whether the object is used
<SCRIPT type = "text/JavaScript">
Linenum
// <! [CDATA [
Document. Write (isobject (new object (), '<br/> ');
Document. Write (isobject (10), '<br/> ');
Function isobject (OBJ ){
Return (typeof OBJ = 'object') & obj. constructor = object;
}
//]>
</SCRIPT>

Purpose:

Very powerful and practical. It can be used to determine whether the JSON string returned by the server is legal. if the server returns an array-type JSON string, the object after eval () is not an array, which indicates that the server has encountered an error.

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.