Home >Web Front-end >JS Tutorial >Five basic data types in javascript_javascript skills

Five basic data types in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:42:331398browse

[0] 5 data types:

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

           [0.1.1] Basic type values ​​refer to simple data segments, the 5 basic types are accessed by value, because the actual value stored in the variable can be manipulated

                                                                         [0.1.2] Basic type values ​​occupy a fixed size of space in the memory and are stored in the stack memory. Copying a value of a primitive type from one variable to another creates a copy of the value.

                                                                                  [0.1.3] Cannot add attributes 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 the location in the memory, that is, the memory space of the operation object cannot be directly accessed. When you manipulate an object, you are actually manipulating a reference to the object rather than the actual object.

                                                                                                                                                                                        The value of the reference type may be an object, which can be stored in the heap memory. The variable containing the reference type value actually contains not the object itself, but a pointer to the object. Copying a reference type value from one variable to another actually copies a pointer, so both variables ultimately point to the same object.

                                                      [0.2.3] For reference type values, you can add attributes and methods to them, and you can also change and delete their attributes and methods

[1]Undefined

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

[1.2]var a 96b4fef55684b9312718d5de63fb7121 var a = undefined;

[1.3] For variables that have not been declared, you can only perform one operation, which is to use the typeof operator to detect its data type [but it will cause an error in strict mode]

[1.4] Appearance scene:

                                                                                                                 [1.4.1] Declared unassigned variable

                                                                                                                                 Get the non-existent attributes of the object

         [1.4.3] Execution results of functions without return values

         [1.4.4] The parameters of the function are not passed in

[1.4.5]void(expression)

[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. From a logical perspective, the null value represents a null object pointer

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

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

[2.4] Occurrence scenario: when the object does not exist

[2.5]Type conversion

Boolean(null):false

Number(null):0

String(null):'null'

[Note 1] null is a null object pointer, [] is an empty array, {} is an empty object, 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] Appearance scene:

                                                                     [3.2.1] Conditional statements cause hermit type conversion to be performed by the system

[3.2.2]Literal or variable definition

[3.3]Type conversion

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

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

[3.4]Boolean()

Boolean(undefined):false

Boolean(null):false

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

Boolean(non-0): true || Boolean(0 and NaN):false

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

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

[4]Number

[4.1] The Number type uses IEEE754 format to represent integers and floating point values ​​

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

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

                                                                                                                                          The first digit must be 0, followed by the octal digit sequence (0-7). If the value in the literal value exceeds the range, then the leading 0 will be ignored, and the following values Parsed as a decimal number

[4.2.2] Octal literals are invalid in strict mode and will cause js to throw an error

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

                                                                                                                                                                                                                  The value in the literal value in hexadecimal system is out of the range. If g, h, etc. appear, an error will be reported

         [4.2.5] When performing arithmetic calculations, all values ​​expressed in octal and hexadecimal will eventually be converted into decimal values ​​

[4.3] Numerical representation:

[4.3.1] Positive 0 and negative 0 can be stored in js 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 point.

                  [4.3.2.1] Since floating-point values ​​require twice the memory space to store integer values, js will convert the floating-point values ​​into integer values ​​without losing any opportunity. If there is no number or floating-point value after the decimal point, It represents an integer, and this value will be saved as an integer value.

                                                          [4.3.2.2] The highest precision of floating point values ​​is 17 decimal places

                                     [4.3.2.3] For very large or very small numbers, they can be represented by floating point values ​​represented by scientific notation e

              [4.3.2.4] By default, js will convert floating point values ​​with more than 6 zeros after the decimal point into values ​​expressed in e notation

                                                                                     [4.3.2.5] A common problem with floating-point calculations based on IEEE754 numerical values ​​is the problem of rounding errors. Such as: 0.1 0.2 === 0.3(15 0)4

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

                                                                       [4.3.3.1] If it exceeds the range of positive numbers, Infinity (positive infinity) is output, and if it exceeds the range of negative numbers, -Infinity (negative infinity) is output

                                 [4.3.3.2] -Infinity cannot participate in numerical calculations

[4.3.3.3] number.max_value 1! = Infinity, because the computer is stored at most 52 -bit digital digits, it cannot be stored more than 1,000, and the accuracy has long been lost, that is, the decimal position is all 0, so the addition is unchanged. >

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

           [4.3.3.5] You can use isFinite() to determine whether a value is finite, 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 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 type conversion Number()

[4.4] Numeric conversion: Number() can be used for any type, parseInt() and parseFloat are specifically used to convert strings into numbers

[Note 1]Number(), parseInt(), and parseFloat() can accept numbers in various bases, but they are not applicable to strings containing numbers

[Note 2] If the number in Number(), parseInt(), parseFloat() is 1.2, an error will be reported, but if the string is '1.2.', no error will be reported

[4.4.1]Number()

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

Number (numbers in various bases): The decimal number after operation, such as 1.0 or 1. or 01 will be output as 1

Number(undefined):NaN

Number(null):0

Number (string):

Number (a string containing only decimal and hexadecimal numbers): decimal number after operation

[Note] The eight -proof is not recognized in the string, and the decimal number processing

Number('' and ' '):0

Number (string in other cases): NaN

Number (object):

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

Number([number]): Number after operation

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

[4.4.2] parseInt(): When converting a string, spaces in front of the string are ignored until the first non-space character is found. If the first character is not a numeric character or a negative sign, parseInt() returns NaN. If so, parsing continues until parsing is complete or a non-numeric character is encountered.

              [4.4.2.1] parseInt() can identify integers of various bases, but when parsing octal literal strings, ECMAScript3 will parse octal, but ECMAScript5 does not have the ability to parse octal

                       [4.4.2.2] The parseInt() function provides a second parameter, indicating the base number, such as: parseInt('123', 16 or 10 or 2)

                                       [4.4.2.3] parseInt (numbers in various bases): decimal numbers after operation, such as 1.0 or 1. or 01 will be output as 1

                                                                                                                                                                                Because parseInt() is specially used to process string conversion numbers, so parseInt (other types include '')//NaN

          [4.4.3] parseFloat(): Similar to parseInt(), it will ignore spaces in front of the string until the first non-space character

is found

                    [4.4.3.1] parseFloat() can only parse decimal strings

                      [4.4.3.2] parseFloat (numbers in various bases): decimal numbers after operation, such as 1.0 or 1. or 01 will be output as 1

[5]String: A character sequence enclosed by single 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 break

t Tabulation

b space

r Enter

f Feed paper

\ slash

        ' Single quote

“ Double quotes

xnn represents a character in hexadecimal nn (n is 0-f), such as x41 represents 'A'

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

[5.2] Strings in ECMAScript are immutable

[5.3] String concatenation requires first creating a new string, then filling the new string with two strings that need to be spliced, and finally destroying the original string. This process occurs in the background and is also the reason why string splicing is slow in some older browsers (IE6), but this inefficiency problem has since 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 Use this method to pass base numbers 2, 8, 10, 16, such as var num = 10;num.toString(2);//1010

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

[5.4.2]String()

There is toString() method, use 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 the array is null or undefined, then the value will be an empty string in the results returned by the join(), toLocaleString(), toString() and valueOf() methods. means

Finally, I will give you a simple example to illustrate the differences between 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"

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn