JS Data type Check code (1/4)

Source: Internet
Author: User
Tags strlen

The results of type checking for different types of objects, respectively, using the two methods I described. The first column of the table shows the objects that we are trying to find their type. Each two column is run typeof variable (variable is the value shown in the first column). All the results in this column are strings. Finally, the third column shows the results of running variable.constructor on the objects contained in the first column. All the results in some columns are objects.

Table 1. Variable type check

———————————————————————————————
Variable typeof variable Variable.constructor
———————————————————————————————
{an: "Object"} Object
["An", "array"] Object array
function () {} function function
"A string" string string
Number number
True Boolean Boolean
New user () object user
——————————————————————————————————

Using the constructor of a variable as a reference to an object type may be the simplest type of checking method


Check if our numbers are actually a string
if (Num.constructor = = string)
If it is, parse it into a number
num = parseint (num);
Check whether our string is actually an array
if (Str.constructor = = array)
If so, connect the array with a comma and get a string
str = Str.join (', ');

Gets the byte length of the string
*/
Java code

1. function strlen (str)
2. {
3. var i;
4. var Len;
5.
6. Len = 0;
7. for (i=0;i<str.length;i++)
8. {
9. If (Str.charcodeat (i) >255) len+=2; else len++;
10.}
One. return len;
12.}
13.

function strlen (str)
{
var i;
var Len;

len = 0;
for (i=0;i<str.length;i++)
{
if (Str.charcodeat (i) >255) len+=2; else len++;
}
return Len;
}

Home 1 2 3 4 last page

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.