Detailed Explanation of javascript Basic Data Types
- 2021-12-05 05:09:03
- OfStack
1. Data types
1.1 Why do you need data types
In computer, different data occupy different storage space. In order to divide data into data with different memory size and make full use of storage space, different data types are defined.
1.2 Data Type of Variable
JavaScript is a weakly typed or dynamic language, which means that the data type of variables does not need to be declared in advance, and the type will be automatically determined during the program running. (The variable type of js is determined only by the data type of the value to the right of the equal sign during the running of the program.)
var age = 10; // This is 1 Digit type
var myName = 'lili'; // This is 1 String type data
1.3 Classification of data types
JS divides data types into two categories:
Simple Data Type (Number, String, Boolean, Undefined, Null) Complex Data Type (object)
2. Simple data types (basic data types)
The simple data types in JavaScript and their descriptions are as follows:
简单数据类型 | 说明 | 默认值 |
---|---|---|
Number | 数字型,包含数值型和浮点型,如 20, 0.12 | 0 |
Boolean | 布尔型,如true、false等价于1和0 | false |
String | 字符串类型,字符串带引号 | " " |
Undefined | var a;声明了变量a但是没有给值,此时a=undefined | undefined |
Null | var a = null;声明了变量为空值 | null |
2.1 Digital Number
1. Digital binary
Common binary, octal, decimal and 106-ary systems
Octal number sequence range: 0 ~ 7 beginning with 0
106-ary digital sequence range: 0 ~ 9 and A ~ F beginning with 0x
2. Digital range
Maximum and Minimum Values of Values in JavaScript
alert(Number.MAX_VALUE); //1.7976931348623157e+308
alert(Number.MIN_VALUE); //5e-324
infinity, which stands for infinity, is greater than any data
-infinity, for infinitesimal, less than any data
NaN, Not a number, representing 1 non-value
isNaN()
This method is used to determine non-digits and returns 1 value, false if it is a digit and true if it is not a digit.
2.2 String String
1. String escape characters
Escape characters all begin with\. Common escape characters and their descriptions are as follows:
Explanation of escape character\ n
Meaning of line break newline
转义符 | 解释说明 |
\n |
换行符 newline的意思 |
\\ | 斜杠\ |
\' | ' 单引号 |
\" | " 双引号 |
\t | tab 缩进 |
\b | 空格,b是blank |
2. String length
The length of the whole string can be obtained through the length property of the string
var myname = 'my name is andy';
console.log(myname.length);
2.3 Boolean Boolean
Boolean values have two values: true and false, where true is true and false is false
When Boolean and numeric values are added, the value of true is 1 and the value of false is 0.
console.log(true + 1); //2
console.log(false + 1); //1
3. Data type conversion
3.1 Convert to String
方式 | 说明 | 案例 |
---|---|---|
toString() | 转成字符串 |
var num = 1; alert(num.toString()); |
String()强制转换 | 转成字符串 |
var num = 1; alert(String(num)); |
加号拼接字符串 | 和字符串拼接的结果都是字符串 |
var num = 1; alert(num+"我是字符串"); |
3.2 Convert to Numeric
方式 | 说明 | 案例 |
---|---|---|
parseInt(string)函数 | 将string类型转换为整数数值型 | parseInt('18') |
parseFloat(string)函数 | 将string类型转换为浮点数数值型 | parseFloat('18.88') |
Number()强制转换函数 | 将string类型转换为数值型 | Number('18') |
js隐式转换(- * /) | 利用算数隐式转换为数值型 | '14'-0 |
number() // Convert to numeric type
number( ' 10') // 10
number( ' abc') // NaN
number(true) // 1
number(false) // 0
number(null) // 0
number(undefined) // NaN
parseInt() // Convert to numbers and round down
// From the converted data, get integers from front to back and find 1 I won't look for one anymore, just look for the one that starts with an integer
Code:
parseInt( ' 12.345') // 12
parseInt( ' 12abc') // 12
parseInt( ' abc12') // NaN
parseInt(true) // NaN
parseInt(false) // NaN
parseInt(undefined) // NaN
parseInt(null) // NaN
Note: These characters must have a number and start with a number, otherwise they are all NaN
parseFloat() // Convert to numbers, integers, decimals
Code:
parseFloat( ' 12.345') // 12.345
parseFloat( ' 12.345abc') // 12.345
parseFloat( ' abc12.345') // NaN
parseFloate(true) // NaN
parseFloat(false) // NaN
parseFloat(undefined) // NaN
parseFloat(null) // NaN
Note: These characters must have a number and start with a number, otherwise they are all NaN
Implicit conversion
1. When one of the left and right sides of + is a string, the other will be quietly converted into a string for splicing
2. Mathematical symbols convert both left and right sides into numbers for arithmetic operations-when there is a string on the left and right sides, the + sign will be spliced, and when there is no string on the left and right sides, + can also convert the left and right sides into numbers
3. When one of the left and right sides of the comparison operator is a number, the other one will be quietly converted into a number for size comparison
3.3 Convert to Boolean
Null and negative values are converted to false, such as', 0, NaN, null, undefined. The remaining values are converted to true string to Boolean type. Null string is false, and others are true
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!