ES6 JavaScript 数字类型、算术运算符、三元运算符和位运算 详解

1、数字常量 Number Constant Properties

// Number Constant Properties
var biggsetNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var posInfiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;

console.log('biggsetNum', biggsetNum)
// biggsetNum 1.7976931348623157e+308

console.log('smallestNum', smallestNum)
// smallestNum 5e-324

console.log('posInfiniteNum', posInfiniteNum)
// posInfiniteNum Infinity

console.log('negInfiniteNum', negInfiniteNum)
// negInfiniteNum -Infinity

console.log('notANum', notANum)
// notANum NaN

2、数字类型表示方法

// 二进制数    binary number
console.log(0b1, 0B11)  // 1 3

// 八进制数    octal number
console.log(0o7, 0O77)  // 7 63

// 十六进制数  hexadecimal number 
console.log(0xF, 0XFF)  // 15 255

// 十进制数    decimal number
console.log(123, 123)   // 123 123

// 指数 exponent(or index) number
console.log(1E3)   // 1000
console.log(2e-2)  // 0.02

3、数字方法 number method

// 把字符串参数解析成浮点数
console.log(Number.parseFloat('1.22'), Number.parseFloat('2.555'), Number.parseFloat(3.99))
// 1.22 2.555 3.99

// 将字符串解析成特定基数对应的整型数字
console.log(Number.parseInt('1.22'), Number.parseInt('2.555'), Number.parseInt(3.99))
// 1 2 3

// 判断传递的值是否为有限数字
console.log(Number.isFinite(123), Number.isFinite('123'), Number.isFinite(1/0))
// true false false

// 判断传递的值是否为整数
console.log(Number.isInteger(2), Number.isInteger('2'), Number.isInteger(2.11))
// true false false

// 判断传递的值是否为NaN。NaN唯一判断方式
console.log(Number.isNaN(Number.NaN), Number.isNaN(12))
// true false

4、Math 方法 Math Method

console.log(Math.PI)            // 3.141592653589793
console.log(Math.abs(-1))       // 1
console.log(Math.log2(16))      // 4
console.log(Math.log10(100))    // 2
console.log(Math.sqrt(2))       // 1.4142135623730951
console.log(Math.random())      // (0, 1), 0.8242695705985819

4.1 扩展

// 扩展:[1,100] 内随机整数
// (0, 1) * 99 -> (0, 99) + 1 -> [1, 100]
for (var i=0; i<3; i++)
    console.log(Number.parseInt((Math.random() * 100) + 1 ))
// 22 75 61

5、算术运算符 Arithmetic operator

console.log(1/2, 2/1, 1/0)
// 0.5 2 Infinity

console.log(5%3, 2%3, 9%3)
// 2 2 0

console.log(parseInt(1/2), parseInt(3/2), parseInt(-1/2), parseInt(-3/2))
// 0 1 -0 -1

console.log(Math.floor(1/2), Math.floor(3/2), Math.floor(-1/2), Math.floor(-3/2))
// 0 1 -1 -2

console.log(Math.ceil(1/2), Math.ceil(3/2), Math.ceil(-1/2), Math.ceil((-3/2)))
// 1 2 -0 -1

console.log(Math.round(1/2), Math.round(3/2), Math.round(-1/2), Math.round(-3/2))
// 1 2 -0 -1

6、位运算 Bit Operations

// ~ 取反, 0变1, 1变0,其实就是对数字求负,然后减1
console.log(1&0, 0&1, 1&1, 0&0)   // 0 0 1 0
console.log(1|0, 0|1, 1|1, 0|0)   // 1 1 1 0
console.log(1^0, 0^1, 1^1, 0^0)   // 1 1 0 0
console.log(~(-2), ~(-1), ~0, ~1, ~2, ~111)  // 1 0 -1 -2 -3 -112
console.log(2<<5, -2<<5)          // 64 -64
console.log(64>>5, -64>>5)        // 2 -2

7、三元运算符 Ternary operator

// 三元运算符 ternary operator
console.log((3 > '30')?"True":"False")    // False
console.log((33 > '30')?"True":"False")   // True

猜你喜欢

转载自blog.csdn.net/weixin_44983653/article/details/107466477