Why does data need a type? The new guy never gets it
Numbers and Strings
Why do we have a 1 and a ‘1’?
Function of different
-
Numbers are numbers, strings are strings. Be careful
-
Numbers can add, subtract, multiply and divide, but strings cannot
-
Strings can represent phone numbers, not numbers
Different types of storage
- In JS, numbers are stored as 64-bit floating point numbers
- In JS, strings are stored in a form like UTF8 (UCS-2)
How to Store numbers
Convert decimal to binary
binary
10 to 2
- 31 becomes binary
- After some trial
- So 31(decimal) = 01 1111(binary)
- Isn’t it a formula? Programmers never use formulas
2 to 10
- The value of 100011 is decimal
- Multiply each of them by 2 to the N, and then add them up
- 100011 = 2^5 + 2^1 + 2^0 = 35
Binary is represented in hexadecimal
Why hexadecimal
- Because binary is too slow to write: 011110001011010
- Remember 8, 4, 2, 1 for X, X, X, X
- Every four bits from right to left are changed to 011110001011010
- Get 3,12,5,10; Change the number greater than 9 to ABCDEF
- So I get 3C5A
- You can also use calculator programmer mode
- HEX indicates hexadecimal and BIN indicates binary
- OCT means base 8, DEC means base 10
How to store characters
Why don’t you just turn it into a number? But there are thousands of characters
How do I store a, B, C, 1, 2, 3
Serial number
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
- Keep in mind that
0 is 48; A is 65; A is equal to 97
How to express Chinese
The serial number means China National Bureau of Standards
Name: “GB 2312”
Use 0000 to FFFF to represent Chinese characters
A hexadecimal number has four 0/1 bits
FFFF is 4×4=16 bits, which is two bytes
Max. 2^16 = 65536 characters
But it contains only over 6,000 Chinese characters, Western alphabet and Japanese kana
The GB2312 number of you is C4E3
GB2312 of “cow” is numbered C5A3
Chinese people have rare characters in their names
Li 16 => Li *
How to express the rare characters, traditional characters, Korean, forgot to include the previous
Microsoft stepped in
Microsoft has launched a gb extension, GBK for short, GBK GB extension
Contains 21886 Chinese characters and graphic symbols
It contains almost all Chinese characters used in China, Japan and South Korea
Fully compatible with GB2312
Still using 16 bits (two bytes)
Later, gb launched GB18030 to replace GBK
GB18030 is incompatible with GB2312
How to express other uncoded national languages
Keep numbering and solve the world’s needs at once
Unicode Unicode
- advantages
Has included 130,000 characters (more than 16 bits), universal
It will continue to be expanded and will not stop
The latest version adds only one character – the combination character of reharmony
- disadvantages
Two bytes are not enough; each character needs three or more bytes
It’s not cost-effective to expand all files by 50%
So what?
Using Unicode is lazy when it comes to saving
Utf-8 invented (lowest eight bits)
- The existence of chicken thief
- Utf-8 is a Unicode storage rule, also known as a character encoding rule
Store “A”
The Unicode number of a is 97 and the hexadecimal number is 61
Unicode: 0000000000000000; 01100001
Utf-8 lazy save: 01100001
Three bytes to one byte, than GBK also save
Store “you”
Your Unicode number is 4F60
Unicode: 00000000; 0100111101100000
Figure 111001001011110110100000
It’s still three bytes, no saving, but it saves a little bit on letters
- The 8 in UTF-8 means
At least 8 bits can be used to store one character
The rules of utf-8
- Take “you A” for example
11100100101111011010000001100001
How do I know what character the above represents?
Read 8-bit message 11100100
Three ones are found at the beginning, indicating that the character has three octagons
So read two more 8-bit messages 10111101 10100000
If you don’t want the first 10, you get 0100111101100000
This reverts to your Unicode self:
000000000100111101100000
Read 8 again for message 01100001
Finding that the character starts with 0 indicates that this character is only 8 bits long
This returns the original Unicode A:
000000000000000001100001
If you don’t understand this page, skip it
Just remember the analogy of waiting in line at a restaurant: Ask at the top table (the algorithm was inspired by human activity).
- How does the computer tell the difference between a number 1 and a string 1
Through auxiliary information: such as postfix TXT text is a string, excel table is a number.
JS data type
7 data types (case insensitive)
-
Digital number
-
String string
-
Boolean bool (George Bull is a British mathematician)
-
Symbolic symbol
-
Empty undefined
-
Null null
-
Object the object
Conclusion: four bases, two empty objects
The following are not data types
- An array of
- function
- The date of
They both belong to Object
1. Digital number
writing
- Write it as an integer 1
- Written as a decimal 0.1
- Scientific notation 1.23E4
- Octal notation (used sparingly) 0123 or 00123 or 0o123
- The hexadecimal notation is 0x3F or 0x3F
- Binary 0b11 or 0b11
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) for example, 0/0=NaN
But is it a number (humans currently don’t know what number it is) NaN === NaN =>false
64-bit floating point number
JS The storage form of a number
- 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
- You don’t usually use fixed point numbers
- The decimal number can always be expressed as any number from 1 to 9, but the binary digit can only be 1. So I don’t have to store the 1. I get rid of it
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)
For example: 0.5 storage form 12 ^ 1 1/2 = = > symbol: 0 index: 1 valid number: 0 0.625 storage form 0.5 + 0.125 = = = = = = = = = 12 1/2 + 1/8 + 1 * 2 ^ ^ – 1-3 = > binary
Range and accuracy
Range (ignoring sign bits)
- Full exponent 2^1024, full significant digit to get the maximum binary digit
- Number. MAX_VALUE: 1.7976931348623157 e+308
- The negative direction of the exponent is 2^-1023, the significant number is at least 1, to get the minimum value
- 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
2. String String
Two bytes per character (castrated UTF8)
writing
- Single quotes
‘hello’
- Double quotation marks
“Hello”
- Backquotes ` `
hello
- Pay attention to
Quotes are not part of a string, just as book titles are not part of a book title
What if you want to include single quotes inside single quotes? Use escape
escape
Error writing
- ‘it’s ok’
- The JS engine will think ‘it’ is over and will not understand the rest
- Chrome Chicken thief, written illegal
Correct term
- ‘It’s OK’ // This is the escape
- Double quotation marks “It’s OK”
- The quotation marks
it's ok
Escape – Use a different way to say what you want (memorization required)
Multiline string
- If you want to enter the string
- When there were no back quotes
Write up very troublesome, can see net path tutorial
String properties
Wait a minute, only objects have properties, why do strings have properties why do strings have properties (they shouldn’t) wait until we get to objects
Length of string
- string.length
- ‘123’.length // 3
- ‘\n\r\t’. Length // 3
- ”.length // 0
- ‘ ‘.length // 1
Read characters by subscript
string[index]
- let s = ‘hello’;
- s[0] // “h”
Note that index starts at 0
- S [0] is the first character
Notice index to length
- let s = ‘hello’;
- S [5] // undefined
- s[4] // ‘o’
Base64 transcoding
- window.btoa
A normal string is converted to a Base64 encoded string
- window.atob
The Base64 encoded string is converted to the original string
- Usually used to hide a resume from a job Posting
E-mail: ZmFuZ3lpbmdoYW5nQGZveG1haWwuY29t
window.atob('ZmFuZ3lpbmdoYW5nQGZveG1haWwuY29t')
Copy the code
- Sometimes it’s a way to deceive yourself
The so-called “encryption”, also can fool a part of the layman
3. The Boolean Boolean
True or false
There are only two values true and false,
Pay attention to case. It’s all lower case
The following operators yield bool values
- 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 statements often need to be checked for true or false
if( value ) { … } else { … }
The question
- It’s fine if value is a bool
- What if value is not a bool? Who’s true? Who’s false
- 1 is true or false, 0 is true or false
0 is false (see five Falsy values)
- ‘1’ is true or false, ‘0’ is true or false
All is true
Five Falsy values of JS (by rote)
- Falsy is a value equal to false but not false
- Undefined null 0 NaN ” (empty string, no Spaces!)
- “And” are not the same thing. Again, be careful!
4. Undefined and NULL are two empty types
empty
Why are there two empty
This is the original (la) creation (JI) of JS
The difference between
There is no essential difference between the default value undefined
- Details a
If a variable is declared but not assigned, the default value is undefined, not null
- Details of the second
If a function does not write return, the default return is undefined, not null
- Details of the three
Front-end programmers, by convention, write null values for non-objects as undefined and null values for objects as null, but only by convention
5. Symbol of symbols
Data types that you don’t use very often
See article zhuanlan.zhihu.com/p/22652486 directly
Summary of the six types (case-insensitive)
undefined null number string bool symbol
These are simple types. Object is called a complex type. We’ll see in the next video
Variable declarations
There are three ways to declare
-
Var a = 1 (obsolete declaration)
-
Let a = 1 (variable declaration)
-
Const a = 1 (constant declaration)
-
A = 1 (assign, not declare global variable)
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 way is wrong. Do not declare it like this
Var variable promotion
We’ll talk about it in the title
Interested can look ahead to the network channel tutorial wangdoc.com/javascript/…
The var statement
Just skip
We write code without var
Explain the topic separately before the interview
Let the statement
The rules
- Follow block scope, that is, scope cannot go beyond {}
{
let b = 1
console.log(b)
}
console.log(b)
Copy the code
- Cannot be repeated
Let a = 1 let a = 2 false
- 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 (var changes window properties)
- For loop with LET works wonders (vs VAR)
Const statement
The rules
- Almost the same as let
- There is only one difference: the value must be assigned at declaration, and cannot be changed after assignment
Variable declarations
Specify a value
var a = 1
It also specifies the type (very important)
Var a = 1 number but the value and the type can be arbitrary a = 2 a = ‘string’
The difference between name and ‘name’
The new guy can’t figure it out
Name is a variable (heap exists)
- Variable value, could be ‘name’, could be ‘hello’
- It can be a variable type, either a number or a string
‘name’ is a string constant (in the stack)
Constants are invariants
- ‘name’ must be ‘name’ and cannot be anything else
- The type is a string
Type conversion
number => string
- String(n)
Uppercase String is used to turn something that isn’t a String into a String
N + “most commonly used method: n+ empty string
string => number
- Number (s) of the capital
- parseInt(s) / parseFloat(s)
Turn anything into an integer
The most common method of s-0: s minus zero
- Plus s plus s
x => bool
- Boolean(x)
!!!!! The most common method for x is to take the original Boolean value
x => string
- String(x)
X.tostring () the most common method, suffixed. ToString ()
(1).toString() (1).toString()