Small case for javascript-Data Type

Source: Internet
Author: User
Js data types: Number, String, Boolean, Undefined, Null, and Object. The data we create in js must be one of the above types. 1. the typeof operator js provides the typeof operator to detect the data type. However, the result of the typeof operation is only numbe... Synta.

Js data types: Number, String, Boolean, Undefined, Null, and Object. The data we create in js must be one of the above types.
 
1, typeof Operator
Js provides the typeof operator to detect the data type, but the result of the typeof operation is only one of number, string, boolean, undefined, object, and function, this is inconsistent with the provided data type.
 
2. undefined type
If a variable declaration is of the undefined type and has a value of undefined before being assigned a value.
NOTE: If we operate on a variable that has never been declared, it is also of the undefined type, but an undefined variable error will occur.
Var message;
Alert (typeof message); // undefined
Alert (message); // undefined
// Var message2;
Alert (typeof message2); // undefined
Alert (message2); // Error
 
3. Null type
The null value indicates a null Object Pointer. It also has only one null value.
Alert (typeof null); // object
Alert (null); // null
 
Note that the undefined value is derived from the null value and has the following results:
Alert (undefined = null); // true
 
Despite this, the roles of the two variables are completely different. Therefore, if we determine that a variable is to point to an object, we should assign a value of null before declaring it.
 
4, Boolean Type
There are only two values: true and false, which are case sensitive. That is, var isError = False; an error is returned.
Js provides a Boolean (arg) function to convert other types to the Boolean type. The more interesting and noteworthy feature is that js will control flow statements (such as if) this API automatically calls the Boolean type conversion.
The transformation rules are as follows:
Flase of Boolean, "" of the String type, 0 and NaN of the Number type, null of the Object type, undefined of the undefined type will be considered as false, and all others are true.
 
5. Number Type
It can be used to represent integers and floating-point numbers. integers also include octal (starting with 0), decimal (default), and hexadecimal (starting with 0x ).
If one of the octal values exceeds 7, the preceding 0 is ignored and is considered as decimal. For example, if 0 70 is converted to 56 in decimal format, 0 18 is regarded as 18.

The storage space of floating point values is twice that of integers. js will convert values such as 1.0 and 10 to Integers for saving.
It should be noted that do not use js-based floating-point numeric operations as far as possible:
Alert (0.1 + 0.2); // The output is 0.30000000000000004
 
Note the following special values:
Number. MAX_VALUE and Number. MIN_VALUE store the maximum and minimum supported values. If they exceed this range, positive Infinity and negative Infinity are obtained. We can use the isFinite () function to determine if a number is out of the range. Here, we can obtain positive Infinity and negative Infinity from Number. NEGATIVE_INFINITY and Number. POSITIVE_INFINITY.
 
In addition, there is a value NaN that should be paid attention to. Any operation that needs to return a value will return NaN instead of a value. It is a special value, and NaN will be returned for any operation with any value, and it is not equal to any value including itself.
In addition, the isNaN () function is used to determine whether the input parameter of any type is NaN. IsNaN converts the parameter. If it can be converted to a value, false is returned. in this case, the conversion is an overall conversion, and the conversion from true to 1 and from false to 0. if the parameter is an object, it will first call the valueOf () of the object. If it cannot be converted to a value, it will call toString () again to judge.
Alert (isNaN ("1234 asdf"); // true
Alert (isNaN (0070); // false
Alert (isNaN ("00070"); // false
Alert (isNaN (0x70); // false
Alert (isNaN ("0x70"); // false
Alert (isNaN (false); // false
Alert (isNaN (""); // false
Alert (isNaN (NaN); // true


6. Numerical Conversion: Number (), parseInt (), parseFloat ()
Number () can be converted to any type of parameter. The conversion rules are as follows:
Convert Boolean type to true to 1, and convert false to 0;
Number type, directly returned;
Undefined returns NaN;
String type, empty String to 0, numeric String will ignore all front 0 to the corresponding integer or floating point type, so octal will be ignored, if it is a numeric String starting with ox, convert the hexadecimal number to the corresponding decimal number, and convert the others to NaN;
Object type. If null, 0 is returned. Otherwise, the valueOf () method of the object is called to return the value. If NaN is returned, the toString () method is called to determine the return value.
Alert (Number ("1234 asdf"); // NaN
Alert (Number ("00070"); // 70
Alert (Number ("false"); // 0
Alert (Number (""); // 0
 
ParseInt () and parseFloat only operate on string parameters.
Conversion rules of parseInt:
It ignores all the leading spaces and converts them from the first character that is not a space. If the character is not a number or a negative number, NaN is returned, or the next position is converted, until the next position is not a number or end position. In this case, "1234abcd" will return 1234, "" will return NaN, "12.3" will return 12.
Alert (parseInt ("1234 abcd"); // 1234
Alert (parseInt (""); // NaN
Alert (parseInt ("12.34"); // 12
 
Different from Number (), parseInt () can identify octal and hexadecimal values for corresponding conversion, but there is a problem with the implementation of the browser, IE converts 070 to 70, while ff and chrome converts 070 to 56. When there are more than one 0, it is equivalent to only one 0.
Alert (parseInt ("070"); // ie pop-up 70, ff, chrome pop-up 56
 
Fortunately, javascript provides another parameter to determine the hexadecimal number (2/8/10/16) to be processed.
Alert (parseInt ("070", "8"); // 56 is displayed

Similar to parseFloat () and parseInt (), parseFloat () Checks characters one by one until it is not a numeric character or the second decimal point.
Alert (parseFloat ("1234 abcd"); // 1234
Alert (parseFloat (""); // NaN
Alert (parseFloat ("12.34"); // 12
Alert (parseFloat ("12.34.22"); // 12.34
Alert (parseFloat ("0x23"); // 0
Alert (parseFloat ("0323"); // 323
 
 
6. String type
Almost all types (except null and undefined) have a tostring () method to convert itself into a string type.
 
In addition, the numeric toString () can also accept a parameter (2/8/10/16) to determine the string value in which the hexadecimal representation is used.
For null and undefined, calling toString () will produce an error. If you are not sure whether the value to be converted is null/undefined, you can call String () to convert it. It will convert null to "null", undefined is converted to "undefined". Others are the same as toString.

 
7. Object Type
This type is a set of data and functions. We can use var o = new Object (); or var o = {}; to create an Object.

Each object instance has some methods/attributes:
Constructor attribute: used to save the method for creating the current object.
HasOwnProperty (name): used to check whether the instance of the current object has an attribute.
IsPrototypeOf (object): used to check whether the input object is a prototype of another object.
PropertyIsEnumerable (name): used to detect whether this attribute of an object can be enumerated using for-in.
ToString (): String Representation of the returned object.
Valueof (): returns the string, Boolean, or numeric representation of the object. It is usually the same as the return value of toString.

 

From for certain
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.