fundamental data type
Data types in JS consist of both primitive values and objects, and there are a total of seven primitive values:
- Numeric (Number)
- BigInt
- String (String)
- Boolean
- Null
- Undefined
- Symbol
Numeric values and large integers
Numeric (Number):All integers and floats in js are of type number
let a = 3
let b = 3.1415926
In js, the value is not infinite, when the value exceeds a certain range will show the approximate value, if a little larger, it will show the scientific notation, if the value is larger than the display range, it will be displayed as Infinity, Infinity is a special numerical value to indicate the infinite
// js decimal calculations have certain precision limitations, in most cases the precision is sufficient, for some specific decimal operations, there may be imprecise results
a = 0.1 + 0.3 // 0.300000000000000000004
a = 1 - "2" // NaN (Not a Number) ,NaN is also a special numeric value that represents an illegal value, a numeric value and a string to do the math.
BigInt: Large integers are used to represent some relatively large integers, using the n ending, and can represent an infinite range, depending on the size of the memory, and cannot be used for small numbers.
// Numbers end in n, and large integers cannot be directly manipulated with integers
a = 999999999999999999999n
String
Use single or double quotes for strings
let a = "hello"
let b = 'world'
transferred meaning
String escaping using \backslash
a = "This is a \"string\""
String can not be straight line feed, if the line needs to be + a \
a = "This is a string \
string"
Stencil String
Template strings use inverted single quotes to indicate ``, template strings can be directly line feed, string line feed, space and other effects will be retained
a = `This is a string of
character
string`
Template strings can be embedded in variables, using the ${variable name} syntax
let a = "xiaoli"
let str = `name is ${a}` // name is xiaoli
Boolean
let a = true
let b = false
Null
The null value has only one value null, which is used to represent the empty object, typeof type is object
let a = null
undefined
The effect is similar to the null value, but also has only one value, undefine.
let a // The variable a is undefined if it is not assigned a value.
Symbol
symbol is used to create a symbol that represents a unique identifier.
let a = Symbol()
The original value cannot be modified
Raw values are immutable types, and once created, values cannot be modified.
let a = 100
a = 200 // Checks memory for a value of 200 if it doesn't exist, creates a value of 200, and points a to the memory address of 200 without modifying the original 100.
Type checking
Types checked by js using typeof
typeof checks the type of the value stored in the variable; variables do not have types
let a = 100
(typeof a) // number
type conversion
Convert to String
toString()
let a = 100
() // The effect is to create a new string of 100, not to change the value 100 into a string 100.
(typeof a) // number
a = () // Create a string, assign it to a, and a becomes a string.
(typeof a) // string
null, undefined no toString method, can not be converted to a string, will report an error, the other can be converted to a string through this method
String()
let a = 100
string(a) // a is number
a = String(a) // a is a string, same principle as toString
Using the String(parm) method, null and undefined are converted to the strings null and undefined.
Conversion to numeric values
Number()
let n = "100"
Number(n) // string
n = Number(n) // number
If the source is a legal number, it will be converted normally, if not, it will be converted to NaN.
If it is an empty string or a string containing only spaces, it will be converted to 0
parseInt() / parseFloat
// Only string types are supported, no other types are supported, Number supports other types.
a = parseInt(a) // Convert the string to an integer.
let a = "123pppp"
a = parseInt(a) // a = 123, parses from left to right to get a legal number, terminates when it encounters a non-number, and fails to parse to a number if the string starts with a letter followed by a number.
The difference between parserInt and Number: Number can only parse legal data and types other than strings, parse can only parse strings, but can parse illegal data.
If the incoming parameter is not a string, the parameter will be converted to a string before parsing, and cannot parse the number after the decimal place, if 123.456, it will be parsed as the number 123
a = parseFloat(a) // The logic is the same as parseInt.
Convert to Boolean
digital (electronics etc)
let a = 1
a = Boolean(a) // true
let b = 0
b = Boolean(b) // false
In numbers, all are true except 0 and NaA which are false
a = true // 1
a = false // 0
a = Infinity //true
a = -1 // true
a = 100 //true
a = 0 // flase
a = NaN // false
string (computer science)
Empty strings are false, the rest are true
let a = "" // false
let b = " " //true
The rest of null and undefined are converted to false
objects are usually converted to true
Convert to null and undefined
a = null
a = undefined