First, data types
1.1 an overview of the
JS data type
7 kinds (case doesn’t matter)
- The number of digital
- String string
- Bool Boolean
- Symbol (used rarely)
- Undefined empty
- Null null
- Object the object
- Conclusion: four bases, two empty objects
The following are not data types (both are objects)
- An array of
- function
- The date of
Why does data need a type?
Both the number and the string are 1. Why distinguish the number 1 from the string ‘1’?
Function of different
- Numbers can be added, subtracted, multiplied and divided, 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 similar to UTF8
1.2 Null undefined and Boolean values
1.2.1 Boolean value
There are only two values, true and false
The following operators yield bool values
- Two yuan logical operators: && (And), | | (Or)
- Negative operation! value
- 1==2, 1! = 2, 3 === 4,3! = = 4
- Comparison operations 1>2, 2≤4
If JavaScript expects a location to be a Boolean, existing values at that location are automatically converted to Booleans. The conversion rule is that all values are considered true except for the following six values that are converted to false. The five values other than false are called falsy values.
undefined
null
false
0
NaN
""
or' '
(Empty string)
Booleans are often used to control program flow, often in conjunction with if statements.
if (' ') {
console.log('true');
}
// There is no output
Copy the code
In the code above, the criterion after the if command is expected to be a Boolean value, so JavaScript automatically converts the empty string to a Boolean value false, causing the program to not enter the code block, so there is no output.
Note that the Boolean values for the empty array ([]) and the empty object ({}) are both true.
1.2.2 Undefined and NULL are null types
There is no essential difference. There are a few details
- Detail 1: If a variable is declared but not assigned, the default value is undefined, not null
- Detail 2: If a function does not write return, the default return is undefined, not null
- Detail 3: Front-end programmers are used to writing null values of non-objects as undefined and null values of objects as null, but this is just a habit and can be ignored
Tutorial Text:
Null means null, that is, the value there is now null. When a function is called and no value is set for a parameter, null is passed to indicate that the parameter is null. For example, if a function accepts an error thrown by the engine as an argument, it passes null to indicate that no error occurred if no error occurred during execution.
Undefined means “undefined.” Here is a typical scenario for returning undefined.
// The variable is declared, but not assigned
var i;
i // undefined
// When calling a function, the argument that should be provided is not provided, which is equal to undefined
function f(x) {
return x;
}
f() // undefined
// The object has no assigned attributes
var o = new Object(a); o.p// undefined
If the function returns no value, undefined is returned by default
function f() {}
f() // undefined
Copy the code
1.3 numerical
1.3.1 Integers and floating point numbers
Inside JavaScript, all numbers are stored as 64-bit floating-point numbers, even integers. So, 1 is the same thing as 1.0, it’s the same number. That is, at the bottom of the JavaScript language there are no integers at all, and all numbers are decimals (64-bit floating-point numbers).
Because floating point numbers are not exact values, comparisons and operations involving decimals need to be done with extreme care.
0.1 + 0.2= = =0.3
// false
0.3 / 0.1
/ / 2.9999999999999996
(0.3 - 0.2) = = = (0.2 - 0.1)
// false
Copy the code
1.3.2 Numerical accuracy
The 64 binary bits of JavaScript floating-point numbers, known as 64-bit floating-point numbers, are made up like this, starting from the far left.
- Bit 1: sign bit,
0
For positive numbers,1
According to a negative number - 2nd to 12th (total of 11 places) : index part
- Decimal digits 13 to 64 (52 digits in total)
There are 11 bits in the exponent part, so the size range is 0 to 2047, divided by two to plus or minus, so -1023 to 1024
There are 11 bits in the exponent part, so the size ranges from 0 to 2047. IEEE 754 states that if the value of the exponential part is between 0 and 2047 (excluding both endpoints), the first digit of a significant digit is always 1 by default and is not stored in a 64-bit floating-point number. That is, the significant digit is always 1.xx… In the form of xx, where xx.. Parts of xx are stored in 64-bit floating-point numbers, up to a maximum of 52 bits. As a result, JavaScript provides up to 53 bits of significant digits.
(-1)^ symbol bit *1.xx... xx *2^ Exponential partCopy the code
The above formula is the actual representation of a number in JavaScript under normal conditions (the exponent part is between 0 and 2047).
The accuracy is up to 53 bits, which means that integers with absolute values less than 2 to the power of 53, namely -253 to 253, can be accurately represented.
Math.pow(2.53)
/ / 9007199254740992
Math.pow(2.53) + 1
/ / 9007199254740992
Math.pow(2.53) + 2
/ / 9007199254740994
Math.pow(2.53) + 3
/ / 9007199254740996
Math.pow(2.53) + 4
/ / 9007199254740996
Copy the code
In the code above, integer arithmetic starts to get wrong after 2 to the power of 53. So anything greater than 2 to the power of 53 is not accurate. Since 2 to the power of 53 is a 16-bit decimal number, the simple rule is that JavaScript can handle 15-bit decimal numbers precisely.
Math.pow(2.53)
/ / 9007199254740992
// The extra three significant digits will not be saved
9007199254740992111
/ / 9007199254740992000
Copy the code
As the example above shows, any extra significant digits above 2 to the power of 53 (111 in the last three digits) will not be saved and will become 0.
1.3.3 Numerical range
According to the standard, the length of the exponential part of a 64-bit floating-point number is 11 binary bits, meaning that the maximum exponent part is 2047 (2 to the eleventh minus 1). That is, if the exponential part of a 64-bit floating-point number has a maximum value of 2047, and half of it is negative, JavaScript can represent values from 21024 to 2-1023 (the open range), beyond which numbers cannot be represented.
If a number is greater than or equal to 2 to the power of 1024, then a “forward overflow” occurs, meaning that JavaScript cannot represent such a large number, and Infinity is returned.
Math.pow(2.1024) // Infinity
Copy the code
If a number is less than or equal to 2 to the -1075 (the exponent minimum is -1023, plus 52 decimal places), then a “negative overflow” occurs, meaning that JavaScript cannot represent such a small number, and zero is returned.
Math.pow(2, -1075) / / 0
Copy the code
Here is a practical example.
var x = 0.5;
for(var i = 0; i < 25; i++) {
x = x * x;
}
x / / 0
Copy the code
In the above code, we square 0.5 25 times in a row, and because the result is too close to 0 to be representable, JavaScript simply converts it to 0.
JavaScript provides the MAX_VALUE and MIN_VALUE properties of the Number object, which return the specific maximum and minimum values that can be represented.
Number.MAX_VALUE / / 1.7976931348623157 e+308
Number.MIN_VALUE // 5e-324
Copy the code
1.3.4 Numerical representation
- The integer 1
- The decimal 0.1
- Scientific notation 1.23e4.
- Octal (used less) 0123,00123,0o123
- Hex 0x3F 0x3F (used sparingly)
- Binary 0B11 or 0B11 (used sparingly)
1.3.5 Base of values
When representing a number directly as a literal, JavaScript provides four base representations for integers: decimal, hexadecimal, octal, and binary.
- Decimal: a number without a leading 0.
- Octal: Has a prefix
0o
or0O
Or a number that has a leading 0 and uses only the eight Arabic numerals from 0 to 7. - Hexadecimal: indicates a prefix
0x
or0X
The numerical values. - Binary: prefix
0b
or0B
The numerical values.
By default, JavaScript automatically converts octal, hexadecimal, and binary to decimal. Here are some examples.
0xff / / 255
0o377 / / 255
0b11 / / 3
Copy the code
An error is reported if an octal, hexadecimal, or binary value contains a number that does not belong to that base.
0xzz / / an error
0o88 / / an error
0b22 / / an error
Copy the code
In the code above, the letter Z appears in hexadecimal, the number 8 appears in octal, and the number 2 appears in binary, so an error is reported.
Normally, a value with a leading 0 is considered octal, but if it is followed by the digits 8 and 9, it is considered decimal.
0888 / / 888
0777 / / 511
Copy the code
The leading 0 represents octal, which is easy to mess up. ES5’s Strict mode and ES6 did away with this notation, but browsers continue to support it for compatibility with previous code.
Decimal to binary method:
- 31 is converted to binary, and after some attempts, 31= 02 to the fifth plus 12 to the fourth plus 12 to the third plus 12 squared plus 12 + 1Two to the zero
- So 31 (decimal) = 01111 (binary)
Binary to decimal
- If 100011 is decimal, multiply each digit by 2 to the n and add it up
- 100011 = 2 to the fifth +2 to the first +2 to the zero =35
Binary to hexadecimal
Use a programmer’s calculator, or do it yourself
1.3.6 Special Values
- Plus 0 and minus 0, they’re both equal to 0, but they’re three numbers (1 over minus 0 is minus infinity)
- Infinity: Infiniti, +Infiniti, -Infiniti
- Unexpressible Number: NaN (Not a Number), but he is a Number. (for example, 0 divided by 0 returns NaN.) NaN does not equal any value, including NaN.
1.4 the string
1.4.1 definition
A string is zero or more characters grouped together in single or double quotation marks. (and backquotes)
'abc'
"abc"
`abc`
Copy the code
If you want a carriage return in a string, you can just use backquotes
let s = 'This can be done easily with backquotes.
Copy the code
1.4.2 escape
The backslash (\) is also called an escape character because it has special meaning in a string and is used to indicate special characters.
Special characters that need to be escaped with backslashes are mainly the following.
\ 0
: null (\u0000
)\b
: Back button (\u0008
)\f
: page feed (\u000C
)\n
: line break (\u000A
)\r
: Enter key (\u000D
)\t
: TAB (\u0009
)\v
: Vertical TAB (\u000B
)\ '
: single quotation mark (\u0027
)\"
: double quotation marks (\u0022
)\ \
: backslash (\u005C
)\xHH
:\x
Followed by two hexadecimal numbers (00
toFF
), representing a character.HH
Unicode code points for proper characters, such as\xA9
Represents a copyright symbol. This method can output only 256 characters.\uXXXX
:\u
Followed by four hexadecimal numbers (0000
到FFFF
), representing a character.XXXX
Unicode code points for proper characters, such as\u00A9
Represents a copyright symbol.
These characters are preceded by backslashes to indicate special meanings.
1.4.3 Strings and Arrays
Strings can be thought of as an array of characters, so you can use the square bracket operator of the array to return a character at a position (position numbering starting at 0).
var s = 'hello';
s[0] // "h"
s[1] // "e"
s[4] // "o"
// Use the square brackets operator directly on strings
'hello'[1] // "e"
Copy the code
If the number in square brackets exceeds the length of the string, or if there are no numbers in square brackets at all, undefined is returned.
'abc'[3] // undefined
'abc'[-1] // undefined
'abc'['x'] // undefined
Copy the code
But the similarity between strings and arrays ends there. In fact, you cannot change a single character in a string.
var s = 'hello';
delete s[0];
s // "hello"
s[1] = 'a';
s // "hello"
s[5] = '! ';
s // "hello"
Copy the code
The code above shows that individual characters inside a string cannot be changed, added or deleted, and these operations silently fail.
1.4.4 length attribute
The length property returns the length of the string, which is also unchangeable.
var s = 'hello';
s.length / / 5
'\n\r\t'.length / / 3
' '.length/ / 0
' '.length / / 1
Copy the code
The above code indicates that the length property of the string cannot be changed, but it does not report an error.
Why do strings have attributes? After learning the object, you can solve it.
1.4.5 character set
How to store characters?
All symbols are represented as 0-127, and a record is made of which symbols the user clicked
What happens when Chinese people start using computers? (Previously excluding rare characters, traditional characters, Japanese and Chinese)
National Bureau of Standards of China — National Standard 2312
What about rare characters?
Microsoft came out with GBK
Tibetan and Thai appear on the web page, how to do?
Unicode Unicode
advantages
- Has included 130,000 characters (more than 16 bits), universal
- It will continue to expand in the future
disadvantages
- Two bytes are not enough; each character needs three or more bytes
- It’s not cost-effective to expand all files by 50%
Utf-8 was born
Utf-8 is a Unicode storage rule, also known as a character encoding rule.
That’s how you store characters, right
That’s the record number, and then save the number
Variable declaration
There are three ways to declare
- Var a =1
- Let a =1 new
- Const a =1 must be assigned and cannot be changed
- A =1 is a false statement
Let the statement
- Follow block scope and apply no more than {}
- Cannot duplicate declaration
- It may or may not be assigned
- Must be declared before use
- Globally declared let variables do not become window properties
- The for loop works wonders with let
Const statement
- Same as let rule
- The only difference is that the declaration must be assigned, and the assignment cannot be changed
When you declare a variable, you specify a type, but both the value and type can change at any time
Type conversion
The number changes to string 👇
- String(n)
- n + ”
String changes to number 👇
- Number(s)
- parseInt(s) / parseFloat(s)
- s – 0
X becomes bool 👇
- VBoolean(x)
- !!!!! X
X becomes string 👇
- String(x)
- x.toString()