Host object: an object provided by the JavaScript running platform
window: Parasitic on the browser, BOM, provides browser-related operations
document: Parasitic on the document, DOM, providing web page related operations
Built-in objects: objects defined by ECMAScript that can be used in any ECMAScript environment
Local objects: String, Number, Boolean, Array, Object, Function, Date, Promise,XMLHttpRequest
Built-in objects:Math
Custom object: an object defined by yourself
var obj = {}
2. Introduction
js provides us with some methods for manipulating numbers. They are packaged into a built-in object Math. Math is also called a mathematical object.
We don’t need to pay attention to the calculation methods or logic of certain mathematical operations. We only need to know which method implements this mathematical operation on the Math object.
Use of Math objects: Math.xxx()
3. Math methods and attributes
**Math.random():** Generate a random number between 0 ~ 1, including 0 and excluding 1, the range is: [0, 1)
var num = Math.random();
console.log(num);// 一个随机数
**Math.round():** Round a decimal to an integer
var num1 =10.1;
console.log(Math.round(num1));// 10var num2 =10.6;
console.log(Math.round(num2));// 11
**Math.abs():**Returns the absolute value of the specified number
var num =-10;
console.log(math.abs(num));// 10
**Math.ceil(): **Round up the specified decimal
var num =10.1;
console.log(Math.ceil(num));// 11var num2 =10.9;
console.log(Math.ceil(num2));// 11
**Math.floor():** Round the specified decimal down to the nearest integer
var num =10.1;
console.log(Math.floor(num));// 10var num2 =10.9;
console.log(Math.floor(num2));// 10
**Math.max():** The parameters are multiple numbers, and the return value is the largest number
console.log(Math.max(1,2,3,4,5));// 5
**Math.min():** The parameters are multiple numbers, and the return value is the smallest number
console.log(Math.min(1,2,3,4,5));// 1
**Math.sqrt():** Calculate the square root of the specified number
var res = Math.sqrt(4)
console.log(res);// 2
**Math.pow(base, exponent):** Calculate the result based on the specified base and exponent passed in
var res = Math.pow(2,4)
console.log(res);// 16
**Math.PI:** Get the value of π, 3.1415926…
console.log(Math.PI)// 3.141592653589793
Because of the calculation accuracy of the computer, we can only get 15 digits after the decimal point.
Math.PI is not a method, it is an attribute, so you don’t need to add parentheses when using it.
Math.sin() : Calculate sine. Note that the parameters are radians: interactively convert to radians formula:Math.PI / 180 * deg
Math.cos() : Calculate cosine. Note that the parameter is radians: interactively convert the radians formula:Math.PI / 180 * deg
4. Range random numbers
Using rounding and random number methods, random numbers in a specified range can be calculated