Method of primitive type
A primitive value: There are 7 primitive types: String, number, Bigint, Boolean, symbol, NULL, and undefined.
An object: The ability to store multiple values as properties. {} create an object
Object wrapper
They are called String, Number, Boolean, and Symbol. They offer different approaches.
let str = "Hello";
alert( str.toUpperCase() ); // HELLO
Copy the code
- string
str
Is a primitive value. Therefore, when accessing its properties, a special object containing a string literal is created and has useful methods, such astoUpperCase()
. - This method runs and returns a new string.
- The special object is destroyed, leaving only the original value
str
.
Through a wrapper, primitive types can provide methods.
Null /undefined No method without a wrapper note:
- Do not create with new Number(0), object will be returned, all checks are true
- Instead, the Number(0) method call without new should return the base type directly
Such as:
alert( typeof 0 ); // "number"
alert( typeof new Number(0));// "object"!
let zero = new Number(0);
if (zero) { // Zero is true because it is an object, always true,
alert( "xxx" );
}
// Use the direct call method instead
let zero = Number(0);
if (zero) { // zero => false
alert( "xxxx" );
}
Copy the code
Add attributes to wrapper classes dynamically
let abc = "xxxx";
abc.name = 'jason'; // The wrapper class is still there and can be assigned, but in strict mode an error will be reported
alert(str.name); // The wrapper class has already been destroyed, so undefined
Copy the code
Numeric types
There are two types of number
- A double – precision floating – point number
- BigInt
shorthand
1e3 = 1 * 1000 // "e" multiplies the number by 1 followed by a given number of zeros.
1.23 the e6 = 1.23 * 1000000
let ms = 0.000001;
let ms = 1e-6; // There are 6 zeros to the left of 1 equal to 1/1000000
Copy the code
Hexadecimal, binary and octal numbers
/ / hexadecimal
alert( 0xff ); / / 255
alert( 0xFF ); // 255 (same, case does not affect)
// Binary base 8
let a = 0b11111111; // 二进制形式的 255
let b = 0o377; // Octal 255
alert( a == b ); // true, both sides are the same number, 255
Copy the code
toString(base)
The base parameter refers to what code is converted, maximum 36
let num = 255;
alert( num.toString() ); // The default is 255 decimal
alert( num.toString(16));// hexadecimal ff
alert( num.toString(2));// binary 11111111
Copy the code
Direct digital conversion, you can turn long numbers to short numbers, convenient URL transmission
alert( 123456..toString(36));// 2n9c
// Note that we need to use.. Two points. Since one point js is considered a normal decimal, the recognition of two points js is a special treatment.
/ / equivalent to the
(123456).toString(36)
Copy the code
rounding
Math.floor
Copy the code
Round down: 3.1 becomes 3, and -1.1 becomes -2.
Math.ceil
Copy the code
Round up: 3.1 becomes 4 and -1.1 becomes -1.
Math.round
Copy the code
Round to the nearest whole number: 3.1 becomes 3,3.6 becomes 4, and -1.1 becomes -1.
Math.trunc (IE not supported)Copy the code
Do not round: 3.1 becomes 3, and -1.1 becomes -1.
Keep 2 decimal places
1.2345 = > ` 1.23
// Method 1: integer first, then divide
let num = 1.23456;
alert( Math.floor(num * 100) / 100 ); // 1.23456 -> 123.456 -> 123 -> 1.23
// Method 2:toFixed will - round
let num = 12.34;
alert( num.toFixed(1));/ / "12.3" / /
// The length of the content will add zeros
let num = 12.34;
alert( num.toFixed(5));// "12.34000", add 0 at the end
Copy the code
The length of the problem
Numbers store a number in 64-bit ieEE-754 format:
- Fifty-two bits are used to store these numbers,
- 11 bits are used to store the position of the decimal point (for integers, they are zero),
- 1 bit is used for symbols
// The length is too large
alert( 1e500 ); // Infinity
// Error
alert( 0.1 + 0.2 ); / / 0.30000000000000004
alert( 0.1 + 0.2= =0.3 ); // false
alert( 0.1.toFixed(20));/ / 0.10000000000000000555
// Precision solution
let sum = 0.1 + 0.2;
alert( sum.toFixed(2));/ / 0.30
// Use multiplication to enlarge
alert( (0.1 * 10 + 0.2 * 10) / 10 ); / / 0.3
Copy the code
Js can’t describe a third of it, in mathematics, it’s undescribable, it’s an irrational number.
isNaN
NaN
Represents an error. :
alert( isNaN(NaN));// true
alert( isNaN("str"));// true
NaN cannot be directly compared to NaN. "NaN" is unique
alert( NaN= = =NaN ); // false
Copy the code
isFinite
IsFinite (value) Converts its parameter to a number, whether it is a regular number
alert( isFinite("15"));// true
alert( isFinite("str"));// false because it is a special value: NaN
alert( isFinite(Infinity));// false because it is a special value: Infinity
Copy the code
Object.is
Object.is(NaN.NaN) = = =true //true
ObjectIs (0, -0) = = =false //false // Because the sign bit of the number may be different
Copy the code
ParseInt and parseFloat
alert( parseInt('100px'));/ / 100
alert( parseFloat('12.5 em'));/ / 12.5
alert( parseInt('12.3'));// 12, only the integer part is returned
alert( parseFloat('12.3.4'));// stop reading at the second point
// Can not read the situation
alert( parseInt('a123'));// NaN, the first symbol stopped reading
// In what base is the second argument read
alert( parseInt('0xff'.16));// Hex read, 255
Copy the code
Other Math functions
alert( Math.random() ); / / 0.1234567894322
alert( Math.random() ); / / 0.5435252343232
alert( Math.random() ); / /... (Any random number)
Math.max(a, b, c...)` / `Math.min(a, b, c...)
Copy the code
alert( Math.max(3.5, -10.0.1));/ / 5
alert( Math.min(1.2));/ / 1
Math.pow(n, power)
Copy the code
alert( Math.pow(2.10));// 2 to the 10th power = 1024
Copy the code
The accuracy of the egg pain problem
alert( 1.35.toFixed(1));/ / 1.4
// Actual results
alert( 1.35.toFixed(20));/ / 1.35000000000000008882
alert( 6.35.toFixed(1));/ / 6.3
// Actual results
alert( 6.35.toFixed(20));/ / 6.34999999999999964473
// Solution
alert( Math.round(6.35 * 10) / 10); // 6.35 -> 63.5 -> rounded 64 -> 6.4
// The decimal part 0.5 is actually 1/2. The decimal in the denominator of the integer power of 2 can be accurately represented in the binary number system
Copy the code
Random number from min to Max
alert( random(1.5));/ / 1.2345623452
alert( random(1.5));/ / 3.7894332423
alert( random(1.5));/ / 4.3435234525
function random(min,max) {
return min + Math.random() * (min+max)
}
Copy the code
/ / method
function randomInteger(min, max) {
// Now the range is from (min-0.5) to (Max +0.5)
let rand = min - 0.5 + Math.random() * (max - min + 1);
return Math.round(rand);
}
alert( randomInteger(1.3));2 / / method
function randomInteger(min, max) {
// here rand is from min to (max+1)
let rand = min + Math.random() * (max + 1 - min);
return Math.floor(rand);
}
alert( randomInteger(1.3));Copy the code