javascript 基本数据类型
1.number类型始终是64位的浮点数。
2.整数最高有15位
3.类型转换
let x = "100";
let y = "10";
let z = x / y;
4.NaN
let x = 100 / "Apple";//NaN
NaN is a number: typeof NaN returns number
5.Infinity (or -Infinity)
is the value JavaScript will return if you calculate a number outside the largest possible number.
Infinity is a number: typeof Infinity returns number
6.进制输出
let myNumber = 32;
myNumber.toString(32);
myNumber.toString(16);
myNumber.toString(12);
myNumber.toString(10);
myNumber.toString(8);
myNumber.toString(2);
7.BigInt
To create a BigInt, append n to the end of an integer or call BigInt()
let y = 9999999999999999n;
let y = BigInt(1234567890123456789012345);
Operators that can be used on a JavaScript Number can also be used on a BigInt
let x = 9007199254740995n;
let y = 9007199254740995n;
let z = x * y;
Arithmetic between a BigInt and a Number is not allowed (type conversion lose information).
Unsigned right shift (>>>) can not be done on a BigInt (it does not have a fixed width)
BigInt不能有小数部分。
16,8,2进制的BigInt
let hex = 0x20000000000003n;
let oct = 0o400000000000000003n;
let bin = 0b100000000000000000000000000000000000000000000000000011n;
8.最大整数最小整数
MAX_SAFE_INTEGER
MIN_SAFE_INTEGER
let x = Number.MAX_SAFE_INTEGER;
方法:
Number.isInteger()
Number.isSafeInteger()
9.Number.EPSILON is the difference between the smallest floating point number greater than 1 and 1.
let x = Number.EPSILON;
10.判断数组
Array.isArray()
11.Math类
Math.E // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E
Math.round(x) Returns x rounded to its nearest integer
Math.ceil(x) Returns x rounded up to its nearest integer
Math.floor(x) Returns x rounded down to its nearest integer
Math.trunc(x) Returns the integer part of x (new in ES6)
Math.sign(x) returns if x is negative, null or positive
min-max 不含max
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
min-max 含max
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}

浙公网安备 33010602011771号