JS basic data type and judgment method
Determine if the object is empty?
JS Code
- if (typeof myObj = = "undefined") {
- var myObj = {};
- }
- 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
- Determine whether a data definition can be used
- if (typeof (XX) = = "undefined") {
- }
Third, judge the internal structure of the known type of data respectively
1 to determine if an object is empty
JS Code
- Data
- First, use typeof (data) to print the object
- typeof (data);
- Console.log (typeof (data));
- Then use the. hasOwnProperty (' remain ') to determine if the object data has no attribute remain.
- Data.hasownproperty (' remain ');
- 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
- If a variable is not defined, it can be judged in the following way:
- if (data = = undefined) {
- }
3,function
JS basic data type and judgment method