Five Basic Data Types in javascript _ javascript tips-js tutorial

Source: Internet
Author: User
In javascript, the keywords used to declare variables are var. This is different from other programming languages, but javascript also contains five basic data types (simple data types ), they are: Undefined, Null, Boolean, Number, and String. It also contains a complex data type-Object. We will not discuss complex data types here [0] Five data types:

[0.1] basic data types: Undefined, Null, Boolean, Number, String

[0.1.1] the basic type value refers to a simple data segment. The five basic types are accessed by value, because the actual values saved in the variables can be operated.

[0.1.2] the value of the basic type occupies a fixed size in the memory and is stored in the stack memory. Copying a value of the basic type from one variable to another creates a copy of the value.

[0.1.3] attributes cannot be added to basic type values.

[0.2] reference data type: Object

[0.2.1] reference type values refer to objects that can be composed of multiple values. Js does not allow direct access to locations in the memory, that is, it cannot directly access the memory space of the operation object. When operating on an object, the object is actually referenced rather than the actual object.

[0.2.2] A value of the reference type is an object stored in the heap memory. A variable containing the reference type value actually contains a pointer to the object instead of the object itself. Copying a reference type value from one variable to another is actually a pointer, so both variables eventually point to the same object.

[0.2.3] For reference type values, you can add attributes and methods for them, or change or delete their attributes and methods.

[1] Undefined

[1.1] The Undefined type has only one value, that is, undefined.

[1.2] var a <=> var a = undefined;

[1.3] only one operation can be performed on a variable that has not been declared. The typeof operator is used to check its data type. [However, an error may occur in strict mode]

[1.4] scenarios:

[1.4.1] variables not assigned values have been declared

[1.4.2] retrieving attributes that do not exist in an object

[1.4.3] execution results of functions without return values

[1.4.4] The function parameter is not input

[1.4.5] void (expression)

[1, 1.5] type conversion

Boolean (undefined): false

Number (undefined): NaN

String (undefined): 'undefined'

[2] Null

[2.1] the Null type has only one value, which is null. Logically, the null value indicates a Null object pointer.

[2.2] If the defined variable is used to save the object, it is better to initialize the variable to null.

[2.3] In fact, the undefined value is derived from the null value, so undefined = null

[2.4] scenario: when the object does not exist

[1, 2.5] type conversion

Boolean (null): false

Number (null): 0

String (null): 'null'

[NOTE 1] null is a null Object Pointer, and [] is an empty array, {} is a null object, and the three are different.

[NOTE 2] null cannot add custom attributes

[3] Boolean

[3.1] The Boolean type has only two values: true and false.

[3.2] scenarios:

[3.2.1] implicit conversion caused by conditional statements

[3.2.2] literal or variable definition

[1, 3.3] type conversion

Number (true): 1 | Number (false): 0

String (true): 'true' | String (false): 'false'

[1, 3.4] Boolean ()

Boolean (undefined): false

Boolean (null): false

Boolean (non-empty objects include empty arrays [] and empty objects {}): true

Boolean (not 0): true | Boolean (0 and NaN): false

Boolean (non-empty including space string): true | Boolean (''): false

[Note] true is not necessarily equal to 1, and false is not necessarily equal to 0.

[4] Number

[4.1] numbers use the IEEE754 format to represent integers and floating point values

[Note] You can use a value-0 to convert it into a number.

[4.2] The Three literal formats are decimal, octal, and hexadecimal.

[4.2.1] the first part of the octal value must be 0 and then the octal value sequence (0-7). If the value in the nominal value is out of the range, the leading value 0 will be ignored, the following values are parsed as decimal numbers.

[4.2.2] The octal literal is invalid in strict mode, which may cause js throw errors.

[4.2.3] The first two digits of a hexadecimal literal value must be 0x followed by a hexadecimal numeric sequence. uppercase letters can be written in lowercase.

[4.2.4] the value in the literal value in hexadecimal format goes out of the range. If g or h is displayed, an error is returned.

[4.2.5] During arithmetic computation, all values in octal and hexadecimal notation are eventually converted to decimal values.

[2, 4.3] numeric representation:

[4.3.1] Positive 0 and negative 0 can be saved in js and are considered equal

[4.3.2] floating point value: the value must contain a decimal point and contain at least one digit after the decimal point.

[4.3.2.1] Because the memory space required by floating point values is twice the size of the integer value, js will immediately convert the floating point value to an integer, if the decimal point is not expressed as an integer with any number or floating point value, this value is saved as an integer.

[4.3.2.2] The highest precision of a floating point value is 17 decimal places.

[4.3.2.3] for a very large or extremely small number, we can use the scientific notation e to represent the floating point value.

[4.3.2.4] by default, js converts a floating point value with more than six zeros after the decimal point to a value in e notation.

[4.3.2.5] the common problem of floating point calculation based on IEEE754 is the problem of rounding error. For example: 0.1 + 0.2 = 0.3 (15 0) 4

[4.3.3] The value range in js is Number. MIN_VALUE (5e-324) -- Number. MAX_VALUE (1.7976931348623157e + 308)

[4.3.3.1] If a positive number range is exceeded, the output Infinity (positive Infinity) is exceeded, and the output Infinity (negative Infinity) is exceeded)

[4.3.3.2] +-Infinity cannot be used in Numerical Calculation

[4.3.3.3] Number. MAX_VALUE + 1! = Infinity, because the computer can store a maximum of 52-digit ending digits and cannot store more than 1000 digits. It has long lost its precision, that is, the decimal places are all 0, so the addition remains unchanged.

[4.3.3.4] Number. MIN_VALUE-1! =-Infinity is the same reason, so the result is-1.

[4.3.3.5] isFinite () can be used to determine whether a numeric value is poor, including implicit conversions of Number ()

[4.3.3.6] isFinite (NaN) // false

[4.3.4] NaN

[4.3.4.1] NaN is not equal to any value, including NaN itself

[4.3.4.2] any operation involving NaN will return NaN

[4.3.4.3] isNaN () to determine whether the Number is NaN, including implicit conversions of Number ()

[4.4] numeric conversion: Number () can be used for any type. parseInt () and parseFloat are used to convert a string to a numeric value.

[NOTE 1] numbers (), parseInt (), and parseFloat () can be in hexadecimal notation, but they are not applicable to strings containing digits.

[NOTE 2] If the Number in Number (), parseInt (), and parseFloat () is 1.2, an error is returned, but if the string is '1', no error is returned.

[4.4.1] Number ()

Number (true): 1 | Number (false): 0

Number (various hexadecimal numbers): the decimal Number after the operation, for example, 1.0 or 1. Or 01 will be output as 1

Number (undefined): NaN

Number (null): 0

Number (string ):

Number (only contains digits in decimal and hexadecimal strings): the decimal Number after calculation.

[Note] octal characters in strings are not recognized and processed in decimal format.

Number (''and''): 0

Number (string in other cases): NaN

Number (object ):

Number ([] and [0] and [-0]): 0

Number ([Number]): Number after calculation

Number ([1, 2] And {} and other objects): NaN

[4.4.2] parseInt (): when converting a string, the space before the string is ignored until the first non-space character is found. If the first character is not a numeric character or a negative number, parseInt () returns NaN. If yes, the parsing continues until the parsing is complete or non-numeric characters are encountered.

[4.4.2.1] parseInt () can recognize various hexadecimal integers. However, ECMAScript3 parses octal strings, but ECMAScript5 does not have the ability to parse octal values.

[4.4.2.2] The parseInt () function provides the second parameter, indicating the number of hexadecimal values, for example, parseInt ('20140901', 16, 10, or 2)

[4.4.2.3] parseInt (various hexadecimal numbers): A decimal number after calculation. For example, a decimal number such as 1.0 or 1. Or 01 is output as 1.

[4.4.2.4] Because parseInt () is specially used to process String Conversion numbers, parseInt (other types include '') // NaN

[4.4.3] parseFloat (): similar to parseInt (), spaces in front of the string are ignored until the first non-space character is found.

[4.4.3.1] parseFloat () can only parse decimal strings

[4.4.3.2] parseFloat (various hexadecimal numbers): A decimal number after an operation, for example, 1.0 or 1. Or 01, is output as 1.

[5] String: Character Sequence enclosed by single quotes or double quotes. The length of any String can be obtained by accessing the length attribute.

[5.1] character literal, also called Escape Sequence

\ N line feed

\ T tabulation

\ B space

\ R press ENTER

\ F paper feed

\ Slash

\ 'Single quotes

\ "Double quotation marks

\ Xnn represents A character in hexadecimal nn (n is 0-f), for example, \ x41 represents 'A'

\ Unnnn indicates a Unicode Character in hexadecimal form nnnn (n is 0-f). For example, \ u03a3 indicates the Greek character ε.

[5.2] strings in ECMAScript are unchangeable

[5.3] To connect strings, you must first create a new string, then fill the new string with two strings to be concatenated, and then destroy the original string. This process occurs in the background, which is also the cause of slow String concatenation speed in some earlier versions of browsers (IE6). However, this low efficiency problem has been solved.

[5.4] String Conversion

[5.4.1] toString ()

Null and Undefined do not have this method

Boolean, Object, and String have this method.

Number this method can be used to pass the base Number 2, 8, 10, 16, such as var num = 10; num. toString (2); // 1010

However, 10. toString (2) reports an error because the number cannot be followed by the identifier

[5.4.2] String ()

The toString () method is available, and the toString () method is used.

String (null); // 'null'

String (undefined); // 'undefined'

[5.4.3] to convert a value to a string, you can use the plus sign operator to combine it with an empty string ''.

[5.4.4] if the value of an item in the array is null or undefined, the value is expressed as an empty string in the results returned by the join (), toLocaleString (), toString (), and valueOf () methods.

Finally, we will give you a simple example to illustrate the differences between the five basic types.

var testString = "Hello"; var testBoobean = true; var testUndefined = undefined; var testUndefined1; var testNull = null; var testObject = {a:1}; var testFunction = function(){return;};  alert(testString);//"string" alert(testBoobean);//"boolean" alert(testUndefined);//"undefined" alert(testUndefined1);//"undefined" alert(testUndefined2);//"undefined" alert(testNull);//"object" alert(testObject);//"object" alert(testFunction);//"function"

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.