Numbers and Strings
- 1 is not the same as 1
Different types of storage
- In JS, numbers are stored as 64-bit floating point numbers
- Strings are stored in a form like UTF8 (UCS-2)
How to Store numbers
Decimal to binary
- 31 (decimal) = 01111 (binary)
- I’m going to do it to the power of two
Binary to decimal
- Two to the NTH power
Binary is represented in hexadecimal
- Remember 8, 4, 2, 1 for X, X, X, X
- Every four digits from left to right is one digit
- HEX indicates hexadecimal and BIN indicates binary
- OCT means base 8, DEC means base 10
Image credit: Hungry Man Valley
How to store characters
Denoted by numbers
- 0 indicates the end character
- 10 means newline
- 13 means enter
- 32: space
- 33 to 47 indicate punctuation
- 48 to 57 are number symbols
- 65 to 90 are capital letters
- 97 to 122 are lowercase letters
- 127 indicates the delete key
How to express Chinese
- National Bureau of Standards of China No. : National Standard 2312 (16 bits two bytes)
- Microsoft has pulled out of a national standard extension called GBK.
Unicode
- advantages
- Has included 130,000 characters (more than 16 bits), universal
- It will continue to be expanded and will not stop
- disadvantages
- Two bytes is not enough; each character needs three or more bytes
- All files expanded by 50%
JS data type
7 species (no. 8, BigInt)
- Digital number
- String string
- Boolean bool
- Symbolic symbol
- Empty undefined
- Null null
- Object the object
- Conclusion: four bases, two empty objects
The following are not data types
- Arrays, functions, dates
- They both belong to Object
Boolean value
- true or false
- No operation
! value
- Equal to the operation
- 1 == 2, 1! = 2, 3 == 2, 3 == 3 = = 4
- Comparison operations
- 1 > 2, 1 >= 2, 3 < 4, 3 <= 4
- If with a bool
- if(value){… }else{… }
- Pay attention to
- It’s fine if value is a bool
- If value is not a bool
- All except falsy and false are true
- Five falsy value
- undefined
- null
- 0
- NaN
- ‘ ‘
Undefined and null are two empty types
- There is no essential difference:
- If a variable is not assigned, the default value is undefined, not null
- If a function does not write return, the default return is undefined, not null
- Traditionally, null values for non-objects are written as undefined and null values for objects are written as null
Number (number: 64-bit floating point number)
- Integer written
- 1
- The decimal notation
- 0.1
- Scientific enumeration
- 1.23 e4
- Octal notation (used sparingly)
- 0123 or 00123 or 0O123
- Hexadecimal notation
- 0 x3f x3f or 0
- Binary notation
- 0 bl1 or 0 bl1
Special values
- Plus 0 and minus 0
- They’re all equal to 0. Be careful
- infinity
- Infinity, +Infinity, -infinity
- Unexpressible number
- NaN (Not a Number)
64-bit floating point number
- A form of digital storage
- Floating point is a floating point, which means the decimal point moves around
- 123.456 can be represented as 1.23456e10^2
- It can also be represented as 12345.6e10^-2
- 64-bit stores a number
- Symbol takes 1 bit
- Number 11 in index (-1023~1024)
- Significant digits are 52 digits (the leading 1 is omitted)
Range and accuracy
- Range (ignoring sign bits)
- Exponential and significant digits are pulled to get the maximum binary number
- Number. MAX_VALUE: 1.7976931348623157 e+308
- The negative direction of the index is full, the significant number is at least 1, and the minimum value is obtained
- Number.MIN_VALUE: 5e-324
- Accuracy (significant number)
- A maximum of 52+1 binary digits can represent significant digits
- The decimal equivalent of 2^53 is 9 followed by 15 zeros
- So all 15 significant digits can be represented exactly
- 16-digit significant digits that start with less than 90 are also accurate
- 9110000000000001 will not be saved
String string
writing
- Single quotes
- ‘hello’
- Double quotation marks
- “Hello”
- The quotation marks
hello
escape
- Write it another way to say what you want
- \ ‘said’
- \ “said”
- \n indicates a newline
- \r means enter
- \t represents the TAB character
- \ \ \
- \uFFFF indicates the corresponding Unicode character
- \xFF represents the first 256 Unicode characters
String properties
- Length of string
- ‘123’.length
- ‘\n\r\t’.length
- ”.length
- ‘ ‘.length
- Read characters by subscript
- string[index]
let s = 'hello'; s[0] //"h" Copy the code
- Note that index starts at 0
- Notice index to length
let s = 'hello'
s[5] // undefined
s[4] // 'o'
Copy the code
symbol
Not really, you know
Variable declarations
There are three ways to declare
- var a = 1
- let a = 1
- const a = 1
- a = 1
The difference between
- Var is outdated and unusable
- Let is the new, more rational approach
- Const is an assignment that must be declared and cannot be changed
- This last approach is wrong and cannot be declared as such
Var variable promotion
Let the statement
- The rules
- Follow block scope, that is, use scope beyond {}
- Cannot duplicate declaration
- It may or may not be assigned
- You must declare it before using it; otherwise, an error will be reported
- Globally declared let variables do not become window properties
- The for loop works wonders with let
Const statement
- The rules
- Almost the same as let
- There is only one rule: the value must be assigned at declaration time and cannot be changed after assignment
Variable declarations
- Specify a value
- var a = 1
- The type is also specified
- var a = 1
- But both the value and the type can vary at will
- a = 2
- A = string
Type conversion
number => string
- String(n)
- n + ”
string => number
- Number(s)
- parseInt(s) / parseFloat(s)
- s – 0
- +s
x => bool
- Boolean(x)
- !!!!! x
x => string
- String(x)
- x.toString()
BigInt
- Is a built-in object that represents greater than
2^53 - 1
The integer - Cannot be used for
Math
Methods in objects - Not with anyone
Number
Instance mixed operation, both must be converted to the same type, butBigInt
intoNumber
You lose accuracy
Just a quick note and I’ll fill it in later