What are the js data types

Summary:  JavaScript has dynamic typing, which means that the same variable can be used as different types: When you declare a new variable, you can use the keyword "new" to declare its type; JavaScript variables are objects. When you declare a variable, you create a new object.

JavaScript has dynamic typing, which means the same variable can be used as different types:

When you declare a new variable, you can use the keyword "new" to declare its type; JavaScript variables are objects. When you declare a variable, you create a new object.

JavaScript basic data types

Basic type value refers to a simple data segment. Basic data types include: Undefined, Null, Boolean, Number, String; these five basic types are accessed by value and can manipulate the actual value stored in the variable; the value of the basic type Occupies a fixed-size space in memory and is stored in stack memory. Copying a value of a primitive type from one variable to another creates a copy of the value; you cannot add properties to a value of primitive type

undefined

The Undefined type has only one value, which is undefined; var a <=> var a = undefined, that is: use var to declare variables but not initialize them, and only one operation can be performed on uninitialized and undeclared variables, which is to use the typeof operator to detect Its data type (but will cause an error in strict mode) will return undefined; undefined is derived from null, so both "=="

The scene where undefined appears:

1. A variable has been declared but not assigned a value

2. Get properties of objects that do not exist

3. The execution result of a function with no return value

4. The parameters of the function are not passed in

5.void(expression)

Convert undefined to other types

Boolean(undefined):false

Number(undefined):NaN

String(undefined):'undefined'

null

The Null type has only one value, which is null. From a logical point of view, the null value represents a null object pointer. When using typeof detection, it will return object; if the defined variable will be used to save the object, it is best to initialize the variable to null; Empty a variable by setting its value to null

When null appears:

when the object does not exist

convert null to another type

Boolean(null):false

Number(null):0

String(null):'null'

Note: null is an empty object pointer, [] is an empty array, {} is an empty object, the three are different; null cannot add custom properties

boolean type

Boolean type has only two values: true and false; use boolean() to convert; any non-empty string, non-zero value, non-null object is converted to true, empty string, 0, NaN, null, undefined is converted to false

Boolean appears scene:

1. Conditional statements cause implicit type conversions performed by the system

2. Literal or variable definition

Convert Boolean to other types

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

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

Boolean()

Boolean(undefined):false

Boolean(null):false

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

Boolean (non-zero): true || Boolean (0 sum 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

Numeric type

Number is divided into integer and floating point number

It can be converted to a number by a value of -0

Number has three literal formats: decimal, octal, and hexadecimal

1.八进制字面值的第一位必须是0,然后是八进制数字序列(0-7),如果字面值中的数值超出了范围,那么前导0将被忽略,后面的数值被当作十进制数解析
2.八进制字面量在严格模式下是无效的,会导致js抛出错误
3.十六进制字面值的前两位必须是0x,后跟十六进制数字序列,字母可大写可小写
4.十六进制中字面值中的数值走出范围,如出现g,h等会报错
5.在进行算术计算时,所有以八进制和十六进制表示的数值最终都将被转换成十进制数值

数值表示

1.js中可以保存正0和负0,且被认为相等

2.由于浮点型数值需要的内存空间是保存整数值的两倍,因此js会不失时机地将浮点数值转换成整数值,若小数点后没有跟任何数字或者浮点值本身表示的就是一个整数,这个数值会作为整数值来保存。

3.浮点数值的最高精度是17位小数,缺陷是存在舍入误差和无法测定特定浮点数值

4.默认情况下,js会将小数点后面带有6个0以上的浮点数值转换为以科学技术法(以e为底*10的±n次幂)表示的数值

5.基于IEEE754数值的浮点计算的通病是舍入误差的问题。如:0.1+0.2 === 0.3(15个0)4

6.js中的数值范围是Number.MIN_VALUE(5e-324) —— Number.MAX_VALUE(1.7976931348623157e+308);如果超出正数范围,输出Infinity(正无穷大),超出负数范围,输出-Infinity(负无穷大);+-Infinity不能参与数值计算

7.Number.MAX_VALUE+1 != Infinity,因为计算机最多保存52位尾数位,保存不了1000多位,早就失去精度,即小数位全为0,所以相加不变;Number.MIN_VALUE - 1 != -Infinity,也是同样的原因,所以结果为-1

8.可以用isFinite()来确定一个数值是不是有穷的,包含着隐式类型转换Number()

9.用isFinite()来检测,超出范围用false,合法范围内用true

10.NaN与表示非数值,NaN与任何数值都不等包括它自身,任何涉及NaN的操作都会返回NaN;通过isNaN()来检测,如果可以转化为数值则为false,不能则为true

11.isNaN()中包含着隐式类型转换Number()

数值转换

1.Number()可用于任何类型,parseInt()和parseFloat专门用于把字符串转换成数值

2.Number()、parseInt()、parseFloat()可以接受各种进制的数字,但对于含数字的字符串并不适用

3.Number()、parseInt()、parseFloat()中数字为1.2. 会报错,但字符串为'1.2.'则不会报错

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

Number(各种进制的数字):运算后的十进制的数字,如1.0或1.或01会以1输出

Number(undefined):NaN

Number(null):0

Number(只包含数字的十进制和十六进制的字符串):运算后的十进制的数字;字符串中不识别八进制,按照十进制数字处理

Number(''和' '):0

Number(其他情况的字符串):NaN

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

Number([数字]):运算后的数字

Number([1,2]和{}和其他对象):NaN

5.parseInt():在转换字符串时,会忽略字符串前面的空格,直到找到第一个非空格字符。如果第一个字符不是数字字符或者负号,parseInt()就会返回NaN。如果是,则继续解析,直到解析完成或者遇到非数字字符。

parseInt()可以识别出各种进制的整数,但在解析八进制字面量的字符串,ECMAscript3会解析八进制,但ECMAscript5没有解析八进制的能力

parseInt()函数提供第二个参数,表示多少进制,如:parseInt('123',16或10或2)

parseInt(各种进制的数字):运算后的十进制的数字,如1.0或1.或01会以1输出

因为parseInt()是专门用来处理字符串转换数字的,所以parseInt(其他类型包括'')//NaN

parseFloat():类似于parseInt(),会忽略字符串前面的空格,直到找到第一个非空格字符

parseFloat()只能解析十进制字符串

parseFloat(各种进制的数字):运算后的十进制的数字,如1.0或1.或01会以1输出

字符串类型

原文链接

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326562380&siteId=291194637