JS data type

Source: Internet
Author: User

Data type

JS has a total of six data types, including five basic data types, number, String, Boolean, Null, Undefined, and a complex data type--object type.

typeof

The typeof operator detects the data type of a given variable, and returns a function, in addition to number, String, Boolean, Undefined, and object.

Note: null is returned as an object, because null is considered to be an empty objects reference.

Undefined

There is only one value--undefined; using the keyword var to declare a variable, but not initialized, the variable will get the undefined value by default.

Variables initialized to undefined, uninitialized variables, undeclared variables, and undefined are returned when the typeof operator is executed.

However, the Console.log () undeclared variable will be an error and can be used to detect whether the variable is uninitialized or undeclared.

Null

There is only one value--null; from a logical point of view, a null value represents an empty object pointer.

In any case, it is not necessary to explicitly set the value of a variable to undefined, but as long as the variable that is intended to save the object has not actually saved the object, it should be explicitly kept null, not only the convention of NULL as the null object pointer, It is also easy to distinguish between null and undefined.

The JS authoritative guide-usually considered null to be a unique value of the null type, which can represent numbers, strings, and objects as "no value".

JS Authoritative guide-if you want to assign null or undefined to variables or attributes, or pass them as arguments to a function, the best choice is to use NULL.

Small tips: Assigning null to a variable clears the variable.

var x = 5;

Console.log (x); 5

x = null;

Console.log (x); Null

Boolean

Only two values are--true and false.

All other data types can call the transform function Boolean ()and convert it to the corresponding Boolean value.

The rules are as follows:

String: Any non-empty string converted to true; "" (empty string) converted to false. Converts to true even if there is only one space, which is also a non-empty string.

Number: Any non-0 digits (including infinite) converted to True;0/nan are converted to false.

Object: Convert any object to True;null to false. objects, arrays, functions, as long as they belong to the object data type, are converted to true.

Convert undefined:n/a to true;undefined to false.

Number

JS does not distinguish between integer values and floating-point numbers, and all digits in JS are represented by floating-point values.

JS receives 10-binary, 8-binary, 16-binary integers, which are eventually converted to 10-binary values for calculation.

The 8 binary starts with 0, followed by a sequence of 0~7 digits, and if the value exceeds the range of 0~7, the leading 0 is ignored and parsed as a 10 binary.

Console.log (070); 56

Console.log (079); 79

(Note: The JS standard does not support 8 binary numeric literals.) )

16 binary starts with 0x, followed by 16 digits; A~f is case-sensitive.

Console.log (0xCC); 204

1. Floating-point values

The memory space that holds the floating-point number is twice times the integer value, so if possible JS automatically converts the floating-point value to an integer value.

The precision of floating-point numbers is very high, and you never test floating-point values for a particular value.

var x = 0.1 + 0.2;

Console.log (x); 0.30000000000000004

Console.log (x = = 0.3); False

Use scientific notation to preserve values.

Syntax: [digits][.digits][(E|e) [(+|-)]digits]

var x = 1.233e5;

var y = 1.233e-5;

Console.log (x); 123300

Console.log (y); 0.00001233

2. Range of values

JS can represent a range of values, greater than the maximum value of Infinite, less than the minimum value of-infinite.

If the isfinite () function returns True, it proves to be poor, and the numeric value is between the maximum and the minimum value.

3.NaN

It means non-numeric, not a number; Represents a case where an operand that would have returned a numeric value did not return a numeric value.

Nan Two major characteristics: any operation involving Nan will return Nan;nan and any value is not equal, including the Nan itself.

IsNaN () function, any value that cannot be converted to a number returns true.

The IsNaN () function also applies to objects, and when the IsNaN () function is called based on an object, the valueof () method of the object is called first, then the value returned by the method can be converted to a number, and if not, the ToString () method is called based on the return value, and the return value is tested.

4. Numeric conversions

The transformation function number (), which can be used for any data type, is as follows:

If it is a Boolean value, True or false is converted to 1 or 0, respectively;

If it is a null value, 0 is returned;

If it is a undefined value, it returns Nan;

If it is a string, then:

If the string contains only numbers, it is converted to a 10-digit value, and the number may be preceded by + or-and the leading 0 is omitted. If the number starts with 0, it is parsed as a 8-digit value, and if the number starts with 0x, it is parsed as a 16-binary value. )

If the string contains a valid floating-point value, it is converted to the corresponding floating-point value.

If the string is empty (contains no characters, it can be a space), it is converted to 0.

If the string contains characters other than the above format, it is converted to Nan.

If it is an object. The valueof () method of the object is called, then the returned value is converted according to the preceding rule, and if the result is Nan, the ToString () method of the function is called, and then the returned string value is converted with the preceding rule.

parseint () function parsing rules for strings:

When converting a string, ignores the space in front of the string until the first non-whitespace character is found;

A nan is returned if the first character is not a number or +--that is, converting an empty string with parseint () returns Nan.

If the first character is a numeric character, the second character continues to be resolved until a non-numeric character is encountered (the decimal number is not a numeric character).

The parseint () method recognizes 8-and 16-digit numbers, but may be misunderstood at the time of conversion, passing the second argument to the parseint () method, which identifies the format of the number that needs to be converted.

Console.log (parseint ("115", 10)); The valid numeric characters in the string "115" are in 10 binary format and the output is 118.

Console.log (parseint ("0x115", 16)); 277

String

Strings must be quoted, either single or double quotes.

1. Character literals, also known as escape sequences.

\ nthe line break

\ t Horizontal Tabulation

\v Vertical Tabulation

\b Backspace

\ r Enter

\f Page Change

\ \ Backslash

\ ' single quote or apostrophe

\ "Double quotation marks

\xnn a Latin-1 character represented by a 16-in-code NN

\UNNNN A Unicode character in 16-code notation

2. Characteristics of strings

The strings in JS are immutable, and once created, their values cannot be changed; To change a string saved in a variable, first destroy the original string, and then populate the variable with a new string.

In fact, the base type values are immutable, and the reference type values are variable.

3. String conversions

Call a String () transformation function with any data type, as follows:

If the value has the toString () method, the method is called and the corresponding result is returned.

Returns "NULL" if the value is null.

If the value is undefined, return "undefined".

In addition to the null, undefined data types, number, Boolean, string, and object data types have the ToString () method, which returns the string representation of the corresponding value.

When you call the ToString () method of a number, you can pass a parameter, specifying the value type of the output (10 or other), and the default is 10 binary.

var x = 118;

Console.log (x.tostring (10)); Converts the value 118 to a string, output in 10 format, and the result is "118".

Console.log (X.tostring (16)); "76"

Object

The object in JS is actually a set of data and functions.

You create a custom object by creating an instance of type object and adding properties and methods to it.

In JS, the object type is the basis of all its instances, that is, the properties and methods that object has also exist in more specific instances.

Each instance of object has the following properties and methods:

Constructor

hasOwnProperty (PropertyName)

isPrototypeOf (object)

Propertyisnumerable (PropertyName)

toLocaleString (): Returns the string representation of the object that corresponds to the region where the execution environment is executed.

ToString (): Returns the string representation of the object.

ValueOf (): Returns the string, numeric, and Boolean representation of an object, usually the same as the return value of the ToString () method.

Note: The BOM, Dom object belongs to the host object, is implemented by the host and is provided by the definition, and may or may not inherit object.

JS 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.