JS basic data type and judgment method

Source: Internet
Author: User

JS basic data type and judgment method

Determine if the object is empty?
JS Code
    1. if (typeof myObj = = "undefined") {
    2.     var myObj = {};
    3. }
    4. This is currently the most widely used method of judging whether JavaScript objects exist.


I. Basic data types
JS has a total of six data types: Five simple data types and a complex data type:
Five simple data types include: String, number, Boolean, undefined, Null
A complex data type: Obeject

Self-Summary:
1, several false
Undefined,null, empty string, 0 is equal to false, all can pass! To take the reverse.


Ii. using typeof to detect data types
You can use typeof to detect data types:
"Undefined"--this variable is undefined (the typeof operation for initialized and undeclared variables returns undefined)
"Boolean"--this value is a Boolean value
"String"--this value is a string
"Number"--this value is numeric
"Object"--this value is null or OBEJCT
"Function"--this value is a function
JS Code
    1. Determine whether a data definition can be used
    2. if (typeof (XX) = = "undefined") {
    3. }



Third, judge the internal structure of the known type of data respectively
1 to determine if an object is empty
JS Code
    1. Data
    2. First, use typeof (data) to print the object
    3. typeof (data);
    4. Console.log (typeof (data));
    5. Then use the. hasOwnProperty (' remain ') to determine if the object data has no attribute remain.
    6. Data.hasownproperty (' remain ');
    7. Console.log (Data.hasownproperty (' remain ')); True or False


Programming Tips:
1. If the original intention is to have a variable save an object variable, then you should declare the variable when the assignment is null, which helps to further distinguish between null and undefined. Because alert (null==undefined) is true.
2. Use instanceof to determine whether a variable of a reference type is an object of a certain type when detecting data of the object type. This operator returns false if the data of the scope primitive type is returned;


2,undefined
JS Code
    1. If a variable is not defined, it can be judged in the following way:
    2. if (data = = undefined) {
    3. }


3,function

JS basic data type and judgment method

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.