Five basic data types in JavaScript _javascript tips

Source: Internet
Author: User
Tags numeric value

[0]5 type of data:

[0.1] Basic data type: Undefined, Null, Boolean, number, String

The [0.1.1] base type value refers to a simple data segment in which 5 basic types are accessed by value because the actual values stored in the variable can be manipulated

The value of the [0.1.2] base type occupies a fixed amount of space in memory and is saved in stack memory. Copying a value of the base type from one variable to another creates a copy of the value.

[0.1.3] Cannot add attributes to a value of the base type

[0.2] Reference data type: Object

[0.2.1] Reference type values are objects that can be composed of multiple values. JS does not allow direct access to the location in memory, that is, can not directly access the operating object's memory space. When you manipulate an object, you are actually manipulating the object's reference rather than the actual object.

The value of the [0.2.2] reference type is an object, stored in heap memory, and a variable containing a reference type value actually contains not the object itself, but a pointer to the object. Copying the value of a reference type from one variable to another is actually a pointer, so two variables end up pointing to the same object.

[0.2.3] You can add properties and methods to a value of a reference type, or you can change and delete its properties and methods

[1] Undefined

[1.1] Undefined type has only one value, which is undefined

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

[1.3] For a variable that has not been declared, only one operation can be performed, using the TypeOf operator to detect its data type "but in strict mode will cause errors"

[1.4] The scene appears:

[1.4.1] A variable with an unassigned value declared

[1.4.2] Gets properties that do not exist for the object

[1.4.3] The execution result of a function that has no return value

parameter of [1.4.4] function is not passed in

[1.4.5]void (expression)

[1.5] Type conversions

Boolean (undefined): false

Number (undefined): NaN

String (undefined): ' Undefined '

[2] Null

[2.1] A null type has only one value, which is null, and logically, a null value represents an empty object pointer

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

[2.3] The undefined value is actually derived from a null value, so undefined = = NULL

[2.4] Scene: When the object does not exist

[2.5] Type conversions

Boolean (NULL): False

Number (NULL): 0

String (NULL): ' NULL '

[Note that the 1]null is a null object pointer and [] is an empty array, {} is a null object, and three are not the same

[Note that 2]null cannot add custom properties

[3] Boolean

[3.1] Boolean type has only two values: True and False

[3.2] The scene appears:

The hermit type conversion that the [3.2.1] Conditional statement caused the system to perform

[3.2.2] literal or variable definition

[3.3] Type conversions

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

String (True): ' True ' | | String (False): ' False '

[3.4] Boolean ()

Boolean (undefined): false

Boolean (NULL): False

Boolean (Non-empty object includes empty array [] and empty Object {}): True

Boolean (non 0): true | | Boolean (0 and Nan): false

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

[Note that]true is not necessarily equal to 1,false nor equal to 0

[4] Number

[4.1] The number type uses the IEEE754 format to represent integers and floating-point values

[note] You can use a value-0来 to convert it to a number

[4.2] Three literal formats are decimal, octal, hexadecimal

The first digit of the [4.2.1] octal literal must be 0, followed by the octal number sequence (0-7), and if the value in the literal is out of range, then leading 0 will be ignored, followed by the number being parsed as a decimal

[4.2.2] octal literal is invalid in strict mode, which can cause JS to throw an error

The first two digits of the [4.2.3] hexadecimal literal must be 0x, followed by a hexadecimal sequence of digits, which can be uppercase and lowercase

[4.2.4] The value in the literal value in hexadecimal out of range, such as the occurrence of g,h and so will be the error

[4.2.5] In arithmetic calculations, all values in eight and hexadecimal will eventually be converted to decimal values

[4.3] The numerical value means:

[4.3.1]js can hold positive 0 and minus 0 and are considered equal

[4.3.2] Floating-point value: The value must contain a decimal point, and there must be at least one digit after the decimal.

[4.3.2.1] Because a floating-point value requires twice times the amount of memory required to hold an integer value, JS will lose no chance to convert floating-point values to integer values, if the decimal point does not follow any number or floating-point value itself is an integer, this value will be saved as an integer value.

The highest precision of [4.3.2.2] floating-point numbers is 17 decimal digits

[4.3.2.3] for a maximum or minimum number, it can be represented by a floating-point number expressed in scientific notation E.

[4.3.2.4] By default, JS converts a floating-point number with more than 6 0 after the decimal point to a numeric value expressed in E notation

[4.3.2.5] A common problem with floating-point computations based on IEEE754 values is the issue of rounding errors. such as: 0.1+0.2 = = 0.3 (15 0) 4

[The range of values in 4.3.3]js is Number.min_value (5e-324)--number.max_value (1.7976931348623157e+308)

[4.3.3.1] If out of positive range, output Infinity (positive infinity), exceeding negative range, output-infinity (negative infinity)

[4.3.3.2]+-infinity cannot participate in numerical calculations

[4.3.3.3] Number.max_value+1!= Infinity, because the computer save up to 52 digits, save not more than 1000, long lost precision, that is, the number of decimal places are 0, so the addition of the same

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

[4.3.3.5] can use Isfinite () to determine whether a value is poor, including implicit type conversion number ()

[4.3.3.6]isfinite (NaN)//false

[4.3.4] NaN

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

[4.3.4.2] Any operation that involves Nan will return Nan

[4.3.4.3]isnan () to determine if this number is Nan and contains the implicit type conversion numbers ()

[4.4] Numeric conversion: number () can be used for any type, parseint () and parsefloat are specifically used to convert strings to numeric values

[Note 1] Number (), parseint (), parsefloat () can accept a variety of numbers, but do not apply to strings that contain numbers

[Note 2] Number (), parseint (), parsefloat () is 1.2. Will complain, but the string is ' 1.2. ' There is no error.

[4.4.1] Number ()

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

Number (a variety of numbers): decimal digits after the operation, such as 1.0 or 1. or 01 to 1 output

Number (undefined): NaN

Number (NULL): 0

Number (String):

Number (contains only decimal and hexadecimal strings for numbers): decimal digits after the operation

[note] octal is not recognized in string, processed in decimal digits

Number (' and '): 0

Number (other case string): NaN

Number (object):

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

Number ([number]): Numbers after the operation

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-whitespace character is found. If the first character is not a numeric character or a minus sign, parseint () returns Nan. If so, continue parsing until the parse is complete or a non-numeric character is encountered.

[4.4.2.1]parseint () can recognize various integers, but in parsing octal literal strings, ECMAScript3 parses octal, but ECMAScript5 does not have the ability to parse octal

The [4.4.2.2]parseint () function provides a second parameter that represents how many systems, such as: parseint (' 123 ', 16 or 10, or 2)

[4.4.2.3]parseint (various numbers): decimal digits after the operation, such as 1.0 or 1. or 01 to 1 output

[4.4.2.4] because parseint () is specifically used to handle string conversion numbers, parseint (other types include ')//nan

[4.4.3]parsefloat (): Similar to parseint (), ignores the space before the string until the first non-whitespace character is found

[4.4.3.1]parsefloat () can only parse decimal strings

[4.4.3.2]parsefloat (various numbers): decimal digits after the operation, such as 1.0 or 1. or 01 to 1 output

[5] String: A sequence of characters enclosed in single or double quotes that can be obtained by accessing the length property of any string

[5.1] character literal, also called escape sequence

\ n Line Change

\ t tab

\b Space

\ r Carriage Return

\f Paper Feed

\ Slash Slash

\ ' Single quotes

\ "Double Quotes

\xnn represents a character in hexadecimal NN (n is 0-f), as \x41 means ' a '

\UNNNN represents a Unicode character in hexadecimal nnnn (n is 0-f), such as \u03a3 representing the Greek character ε

[5.2] The strings in the ECMAScript are immutable

[5.3] string concatenation requires creating a new string, then filling the new string with two strings that need to be spliced, and then destroying the original string. This process occurs in the background and is also the reason for the slow stitching of strings in some older browsers (IE6), but has since solved this inefficiency

[5.4] String conversions

[5.4.1]tostring ()

Null and undefined do not have this method

Boolean, Object, string have this method

Number use this method to pass cardinality 2, 8, 10, 16, such as var num = 10;num.tostring (2);//1010

But 10.toString (2) will complain because the number cannot be followed by the identifier

[5.4.2] String ()

Has the ToString () method, using the ToString () method

String (null);//' null '

String (undefined);//' undefined '

[5.4.3] to convert a value to a string, you can use the plus operator to add it to an empty string '

[5.4.4] If the value of an item in an array is null or undefined, the value is represented in an empty string in the result returned by the join (), tolocalestring (), toString (), and valueof () methods

Finally, let me give you a simple example to illustrate the difference of these 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"

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.