JavaScript Knowledge Point Summary (vi) JavaScript judgment variable data type _javascript tips

Source: Internet
Author: User

Recently done a project, which has about JS data types of processing, search the relevant information on the Internet, and personally tested a variety of data types of judgments, absolute security. The following small series to the specific content summary share to everybody, everybody consults under!

The data type in JS

1. Value type (number): include integers, floating-point numbers.

2. Booleans (Boolean)

3. String type

4. Objects (object)

5. Arrays (Array)

6. Null value (NULL)

7. Not defined (Undefined)

Ii. determine the data type of a variable

1. Value type (number)

The more commonly used methods of judgment are:

function Isnumber (val) {return
typeof val = = ' number ';

But there are some things you can't do, like:

var A;

This is true, as shown in the following illustration:


But in fact the variable A is Nan, and it cannot be used for numeric operations.

So the above function can be modified to:

function Isnumber (val) {return
typeof val = = ' number ' && isfinite (val);

After the modification, the bounce is false, as shown in the following figure:

Incidentally, the JavaScript isfinite () function, the isfinite () function, is used to check whether its arguments are infinite, and to return true if number is finite (or can be converted to a finite number). Otherwise, returns False if number is NaN (non-numeric), or positive or negative infinity.

2. Booleans (Boolean)

Boolean types are simpler to judge, and can be judged in the following ways:


/* Judge variable val is not a Boolean type
/function Isbooleantype (val) {return
typeof val = = "Boolean";

Test code:

<script type= "Text/javascript" >
/* Judging variable val is not a Boolean type
*
/function Isbooleantype (val) {
Return typeof val = = "Boolean";
}
var A;
var B = false;
Alert ("Variable A is the Boolean type of judgment result:" +isbooleantype (a));
Alert ("Variable B is the Boolean type of judgment result:" +isbooleantype (b));

Run Result:

3. Strings (String)

The judgement of the string type is simple and can be judged by the following methods:


/* Determine whether the variable is a string type
/function Isstringtype (val) {return
typeof val = = "string";

Test code:

<script type= "Text/javascript" >
/* Judgment variable is not a string type
*
/function Isstringtype (val) {
Return typeof val = = "string";
}
var A;
var s = "Strtype";
Alert ("Variable A is a string-type judgment Result:" +isstringtype (a));
Alert ("Variable s is the string type of judgment result:" +isstringtype (s));

Run Result:


4. Not defined (Undefined)

Undefined judgment is simpler and can be judged in the following ways:

/* The
judgment variable is not undefined
/function isundefined (val) {return
typeof val = = "undefined";
}

Test code:

<script type= "Text/javascript" >
var a;//a is undefined
var s = "Strtype";
/* The
judgment variable is not undefined
/function isundefined (val) {return
typeof val = = "undefined";
}
Alert ("Variable A" is the undefined judgment result: "+isundefined (a));
" Alert ("Variable s is undefined the result of the judgment is:" +isundefined (s));

Run Result:


5. Objects (object)

Because the TypeOf returns object when the variable is null, the object cannot be judged directly by TypeOf.

It should be like this:

function Isobj (str) {
if (str = = NULL | | typeof str = = ' undefined ') {return
false;
}
return typeof str = = ' object ';

Test code:

<script type= "Text/javascript" >
/* Judgment variable is not object
type
/function isobj (str) {
if (str = = null | | typeof str = = ' undefined ') {return
false;
}
return typeof str = = ' object ';
}
var A;
var b = null;
var c = "Str";
var d = {};
var e = new Object ();
Alert ("B" is the value of null,typeof b = = ' object ' is the result of: "+ (typeof b = = ' object '));
The result of alert ("Variable A is the type of object is:" +isobj (a));//false
alert ("Variable B is the result of the type of object is:" +isobj (b));//false
alert (" The result of the variable C is the type of object is: "+isobj (c));//false
alert (" Variable d is the type of the result is: "+isobj (d));//true
alert (" The result of the variable e is the type of the object: "+isobj (e));//true

Run Result:


6. Null value (NULL)

To determine null values with val = = null

Function IsNull (val) {return
val = = null;
}

Test code:


/* The judgment variable is not NULL/
function IsNull (val) {return
val = = null;
}
/* Test variable *
var A;
var b = null;
var c = "Str";
Pop-up run result
alert (the result of "variable A is null" is: "+isnull (a));//false
alert (" Variable B is a null-type judgment result: "+isnull (b));//true

Run Result:


7. Arrays (Array)

Array types cannot be judged by typeof. Because when the variable is an array type, TypeOf returns object.

 Here are two ways to determine the type of an array:

/* Judging variable arr is not an array
method one
/function IsArray (arr) {return
Object.prototype.toString.apply (arr) = = ' [Object Array] ';
}
/* Judgment variable arr is not an array
method two
/function IsArray (arr) {
if (arr = = NULL | | typeof arr = = ' undefined ') {
return false;
}
return arr.constructor = = Array;

Test code:

<script type= "Text/javascript" >/* Judgment variable arr is not an array method one/function IsArray (arr) {return
Object.prototype.toString.apply (arr) = = ' [Object Array] '; }/* Judgment variable arr is not an array method two/function IsArray (arr) {if (arr = = NULL | | typeof arr = = ' undefined ') {return false;} return arr
. constructor = = Array;
}//Test variable var a = null;
var B = "";
var C;
var arr = [,,];
var arr = new Array ();
Print test results document.write ("Arr variable is an array type, typeof arr = = = The result of ' object ' is:" + (typeof arr = = ' object '));
document.write ("<br/>"); document.write ("-----------------------------------------------------------------------------------------------
-");
document.write ("<br/>");
document.write ("Use the IsArray method to judge the result is as follows:");
document.write ("<br/>"); document.write ("-----------------------------------------------------------------------------------------------
-");
document.write ("<br/>");
document.write ("Variable A is the array type of judgment result:" +isarray (a));
document.write ("<br/>");
document.write ("Variable B" is the result of the array type: "+isarray (b));" Document.wRite ("<br/>");
document.write ("Variable c is the array type of judgment result:" +isarray (c));
document.write ("<br/>");
document.write ("Variable arr is the array type of judgment result:" +isarray (arr));
document.write ("<br/>");
document.write ("Variable arr is the array type of judgment result:" +isarray (arr));
document.write ("<br/>"); document.write ("-----------------------------------------------------------------------------------------------
-");
document.write ("<br/>");
document.write ("Use the IsArray method to judge the result is as follows:");
document.write ("<br/>"); document.write ("-----------------------------------------------------------------------------------------------
-");
document.write ("<br/>");
document.write ("Variable A is the array type of judgment result:" +isarray (a));
document.write ("<br/>");
document.write ("Variable B" is the result of the array type: "+isarray (b));"
document.write ("<br/>");
document.write ("Variable c is the array type of judgment result:" +isarray (c));
document.write ("<br/>");
document.write ("Variable arr is the array type of judgment result:" +isarray (arr));
document.write ("<br/>");
document.write ("Variable arr is the array type of judgment result:" +isarray (arr)); document.write ("<br/>");  </script>

Run Result:

  

The above content is small series to introduce the JavaScript Knowledge Point Summary (vi) JavaScript judgment variable data types of knowledge, hope to help everyone, if you want to know more content please pay attention to cloud Habitat community website!

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.