The data type in JS
1. Numeric (number): Includes integers, floating-point numbers.
2. Boolean Type (Boolean)
3. String type
4. Objects (object)
5. Arrays (Array)
6. Null value (NULL)
7. Undefined (Undefined)
Second, determine the data type of a variable 1. Numeric type (number)
The more common methods of judging are:
1 function Isnumber (val) {2 return typeof val = = = ' number '; 3}
But there are some things that are not possible, such as:
1 var a;2 alert (Isnumber (parseint (a)));
The bounce here is true, as shown in:
But actually the variable A is Nan, and it can't be used for numeric operations.
So the above function can be modified to:
1 function Isnumber (val) {2 return typeof val = = = ' Number ' && isfinite (val); 3}
When modified, the bounce is false, as shown in:
By the way, the JavaScript isfinite () function, theisfinite () function, is used to check whether its arguments are infinite, or true if number is a finite digit (or convertible to a finite number). Otherwise, if number is NaN (not a number), or positive, negative infinity, then false is returned.
2. Boolean Type (Boolean)
Boolean type judgment is relatively simple, can be judged by the following methods:
1/*2 judgment variable val is not a Boolean type 3 */4 function Isbooleantype (val) {5 return typeof val = = = "Boolean"; 6}
Test code:
1 <script type= "Text/javascript" > 2 /* 3 Judge variable val is not a Boolean type 4 * /5 function Isbooleantype (val) {6 return typeof val = = = "Boolean"; 7 } 8 var A; 9 var b = false;10 alert ("Variable A is a Boolean type, the result is:" +isbooleantype (a)); alert ("Variable B is the Boolean type, the result is:" + Isbooleantype (b)); </script>
Operation Result:
3. Strings (String)
The judgment of the string type is relatively simple and can be judged by the following methods:
1/*2 judgment variable is not a String Type 3 */4 function Isstringtype (val) {5 return typeof val = = = "string"; 6}
Test code:
1 <script type= "Text/javascript" > 2 /* 3 The judging variable is not a string type 4 * /5 function Isstringtype (val) {6 Return typeof val = = = "string"; 7 } 8 9 var a;10 var s = "Strtype"; alert ("Variable A is the result of a string type:" +isstringtype (a)); The variable s is the result of a string type: "+isstringtype (s)"); </script>
Operation Result:
4. Undefined (Undefined)
Undefined judgments are relatively simple and can be judged by the following methods:
1/*2 judgment variable is not Undefined3 */4 function isundefined (val) {5 return typeof val = = = "undefined"; 6}
Test code:
1 <script type= "Text/javascript" > 2 var a;//a is undefined 3 var s = "Strtype"; 4/ * 5 The judging variable is not undefined 6 * /7 function isundefined (val) {8 return typeof val = = = "undefined"; 9 }10 Alert ("Variable A is the result of the undefined:" +isundefined (a)); alert ("The variable s is the result of undefined's judgment:" +isundefined (s)); </script>
Operation Result:
5. Objects (object)
Because when a variable is null, typeof also returns object, so object cannot be judged directly with TypeOf.
This should be the case:
1 function isobj (str) {2 if (str = = = = NULL | | typeof str = = = ' undefined ') {3 return false;4 }5 return Typ EOF str = = = ' object '; 6}
Test code:
1 <script type= "Text/javascript" > 2 /* 3 The judging variable is not an object Type 4 * /5 function isobj (str) {6 if ( str = = = NULL | | typeof str = = = ' undefined ') {7 return false; 8 } 9 return typeof str = = = ' object '; ten }11 var a;13
var B = null;14 var c = "str"; var d = {};16 var e = new Object (); The value of "B" is null,typeof b = = = ' object ' Determines the result: ' + (typeof b = = = ' object '), and alert ("Variable A is the object type of the result is:" +isobj (a));//false20 alert (" The result of the variable B is the object type: "+isobj (b)"), and//false21 alert ("Variable c is the result of the object type:" +isobj (c));//false22 alert (" The result of the variable d is the object type: "+isobj (d)");//true23 alert ("The variable e is the object type's judgment result is:" +isobj (e));//true24 </script>
Operation Result:
6. Null value (NULL)
Determine null with val = = = NULL
1 function IsNull (val) {2 return val = = = Null;3}
Test code:
1/* 2 The judging variable is NOT NULL 3 */4 function IsNull (val) {5 return val = = = null; 6} 7/* Test variable */8 var A; 9 var b = null;10 V AR c = "str"; 11//Eject Run result ("variable A is null" is the result of the judgment: "+isnull (a));//false13 alert (" variable B is the null type, the result is: "+isnull (b));// TRUE14 alert ("Variable c is a null type, the result of the judgment is:" +isnull (c));//false
Operation Result:
7. Arrays (Array)
The array type cannot be judged by typeof. Because when the variable is an array type, typeof returns an object.
Here are two ways to determine the array type:
1/* Judgment variable arr is not an array 2 method 13 */4 function IsArray1 (arr) {5 return Object.prototype.toString.apply (arr) = = = ' [Object Arra Y] '; 6} 7 8/* Determine if ARR is not an array of 9 method 210 */11 function IsArray2 (arr) { if (arr = = = NULL | | typeof arr = = = ' undefined ') {13
return false;14 }15 return arr.constructor = = = Array;16}
Test code:
1 <script type= "Text/javascript" > 2/* judgment variable arr is not an array 3 method 14 */5 function IsArray1 (arr) {6 return Object.protot Ype.toString.apply (arr) = = = ' [Object Array] '; 7} 8/* Determine whether the variable arr is array 9 method 210 */11 function IsArray2 (arr) {13 = = = NULL | | typeof arr = = = ' undefined ') Return false;14}15 return arr.constructor = = array;16}17//Test variable var a = null;19 var b = ""; var C; var ARR1 = [1,2,3];22 var arr2 = new Array (); 23//Print test results in document.write ("arr1 variable is an array type, typeof arr1 = = = ' object ' results are:" + (typeof arr1 = = = ' object '); document.write ("<br/>"); document.write ("------------------------------------------ ------------------------------------------------------"), document.write (" <br/> "); document.write (" Use the IsArray1 method to determine the result as follows: "), document.write (" <br/> "), document.write ("-------------------------------------- ----------------------------------------------------------"), document.write (" <br/> "); document.write ( "Variable A is the array type, and the result is:" +iSArray1 (a)); document.write ("<br/>"); document.write ("Variable b is an array type, the result is:" +isarray1 (b)); document.write (" <br/> "); document.write (" Variable c is the result of the array type: "+isarray1 (c)); PNs document.write (" <br/> "); document.write ("Variable arr1 is the result of the array type:" +isarray1 (ARR1)), the document.write ("<br/>"), and the document.write ("variable arr2 is the array type of the judgment result is:" + IsArray1 (ARR2)), document.write ("<br/>"), document.write ("-------------------------------------------- ----------------------------------------------------"), document.write (" <br/> "), document.write (" Use the IsArray2 method to determine the result as follows: "), document.write (" <br/> "), document.write ("-------------------------------------- ----------------------------------------------------------"), document.write (" <br/> "); document.write ( "Variable A is the result of the array type:" +isarray2 (a)); document.write ("<br/>"), document.write ("Variable b is the array type, the result is:" +isarray2 (b)) ; document.write ("<br/>"); document.write ("Variable c is the array type's judgment result:" +isarray2 (c)); document.write ("<br/> "), document.write (" variable arr1 is the result of the array type: "+isarray2 (ARR1)); document.write (" <br/> "); "Variable arr2 is the result of the array type:" +isarray2 (ARR2)); document.write ("<br/>");
Operation Result:
Ext.: http://www.cnblogs.com/xdp-gacl/p/3490065.html
(go) JavaScript judging data type summary