JavaScript judgment variable data type

Source: Internet
Author: User


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)

Two
1. Numeric type (number)
The more common methods of judging are:

function Isnumber (val) {
Return typeof val = = = ' number ';
}


2. Judging the variable Val is not a Boolean type

Function Isbooleantype (val) {
Return typeof val = = = "Boolean";
}


3. Determine if a variable is a string type

Function Isstringtype (val) {
Return typeof val = = = "string";
}


4. Determine if the variable is undefined

Function Isundefined (val) {
Return typeof val = = = "undefined";
}

5. Determine if the variable is not an object

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

6. Determine if the variable is not NULL

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


7. Determine if the variable arr is an array

Method One
function IsArray (arr) {
return Object.prototype.toString.apply (arr) = = = ' [Object Array] ';
}

Method Two
function IsArray (arr) {
if (arr = = = NULL | | typeof arr = = = ' undefined ') {
return false;
}
return Arr.constructor = = = Array;
}

JavaScript judgment variable data type

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.