JS data types include: number, string, bool, symbol, undefined, null, object, bigint.
Summary: “four bases, two empty objects”, and one is bigint is new.
A: Number
Special values for numbers are:
- 0, plus 0, minus 0
- Infinity Infinity, plus Infinity +Infinity, minus Infinity -Infinity.
- Unexpressed number — NaN, but input
NaN===NaN
, there will be afalse
.
A form of digital storage
In JS, numbers are stored as 64-bit floating point numbers. (binary)The exponent part has 11 bits, ranging from -1023 to +1024. The significant digit has 52 bits, and the leading 1 needs to be omitted. For example: 0.5 the storage way is | | – 1 0 0.
Accuracy (significant number)
A maximum of 52+1 binary digits can represent significant digits. The corresponding decimal number is 9 followed by 15 zeros.
So 15 significant digits can be represented exactly, and 16 significant digits can be represented exactly if they start with 90 or less.
Second, string
1, writing
- Single quotation marks’ hi ‘
- Double quotes “hi”
- Backquotes ` `
Quotes are not part of a string
2, escape
For example, ‘it’s OK ‘, the JS engine will think ‘it’ is over.
Correct term
- Escape: preceded by a slash, such as ‘it’s OK ‘
- Put double quotation marks around it: “It’s OK”
- Put back quotes around it
Escape — Use a different way to say what you want
- ‘–‘ single quotes
- “–” double quotation marks
- \ n – a newline
- \ r – press enter
- \t — TAB character
- \ — \ slash
- \uFFFF — The corresponding Unicode character
- \xFF — The first 256 Unicode characters
- Multiline strings: Surrounded by backquotes
3. String length
String. Length – You can see the length of string for example: ‘\n\r\t’. Length // the value is 3
Characters can be read using subscripts (like arrays) for example: let s=’hello’; S [0] // Value is “h”
4. Base64 transcoding
- Window.btoa (string) — A normal string is converted toa Base64 encoded string
- Window.atob (string) — A Base64 encoded string is converted to the original string
Boolean bool (true/false)
Bool — if(value){… }else{… }
But what if value is not a bool property?
Five falsy value
- undefined
- null
- 0
- NaN
- “(empty string)
These five falsy values represent false. (And false)
Undefined and null
Undefined and null are two empty types. The differences are as follows:
- Declare a variable but do not assign a value. The default value is undefined, not null.
- If the function does not 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.
5
Symbol generates a globally unique value. See MDN or Ruan Yifeng: Getting Started with ECMAScript 6.
Variable declaration
Three ways to declare:
var a=1
let a=1
const a=1
Copy the code
Let is the new, more sensible way to declare local variables. A const declaration must be assigned and cannot be changed.
Let declaration and const declaration
Rules:
- Follow block scope and use within {}.
- Cannot duplicate declaration
- It may or may not be assigned
- Must be declared before use
- Globally declared let properties do not become window properties
A const declaration is almost the same as a let declaration, but must be assigned
Type conversion
1, the number = > string
Var n =1
- Method 1: String(n)
- Method 2: n + “(empty string)
I didn’t change the value of n, I just created a new value
2, stringr = > number
Var s = ‘123’
- Method 1: Number(s)
- Method two: S-0
- Method three: +s
- ParseInt (s) — make it an integer
I didn’t change the value of s, I just created a new value.
3, x => bool
- Method 1: Boolean(x)
- Method 2:!! x
4, x => string
- Method 1: String(x)
- Method 2: x.tostring ()