Chapter 8: JavaScript Math
JavaScript provides a built-in Math object that allows you to perform mathematical operations. This chapter covers basic mathematical operations and methods provided by the Math object.
Basic Math
JavaScript supports basic arithmetic operations such as addition, subtraction, multiplication, and division.
Addition
Add two numbers using the + operator.
Example:
let a = 5;
let b = 3;
let sum = a + b;
console.log(sum); // Output: 8
Subtraction
Subtract one number from another using the - operator.
Example:
let a = 10;
let b = 4;
let difference = a - b;
console.log(difference); // Output: 6
Multiplication
Multiply two numbers using the * operator.
Example:
let a = 7;
let b = 6;
let product = a * b;
console.log(product); // Output: 42
Division
Divide one number by another using the / operator.
Example:
let a = 20;
let b = 4;
let quotient = a / b;
console.log(quotient); // Output: 5
Modulus
Find the remainder of division using the % operator.
Example:
let a = 17;
let b = 5;
let remainder = a % b;
console.log(remainder); // Output: 2
Math Methods
The Math object provides various methods and properties to perform more advanced mathematical operations.
Math.round()
Rounds a number to the nearest integer.
Syntax:
Math.round(x);
Example:
let number = 4.7;
let rounded = Math.round(number);
console.log(rounded); // Output: 5
Math.floor()
Rounds a number down to the nearest integer.
Syntax:
Math.floor(x);
Example:
let number = 4.7;
let floored = Math.floor(number);
console.log(floored); // Output: 4
Math.ceil()
Rounds a number up to the nearest integer.
Syntax:
Math.ceil(x);
Example:
let number = 4.3;
let ceiled = Math.ceil(number);
console.log(ceiled); // Output: 5
Math.abs()
Returns the absolute value of a number.
Syntax:
Math.abs(x);
Example:
let number = -7;
let absolute = Math.abs(number);
console.log(absolute); // Output: 7
Math.max()
Returns the largest of zero or more numbers.
Syntax:
Math.max(value1, value2, ..., valueN)
Example:
let max = Math.max(1, 3, 2, 8, 4);
console.log(max); // Output: 8
Math.min()
Returns the smallest of zero or more numbers.
Syntax:
Math.min(value1, value2, ..., valueN)
Example:
let min = Math.min(1, 3, 2, 8, 4);
console.log(min); // Output: 1
Math.sqrt()
Returns the square root of a number.
Syntax:
Math.sqrt(x);
Example:
let number = 16;
let squareRoot = Math.sqrt(number);
console.log(squareRoot); // Output: 4
Math.pow()
Returns the base raised to the exponent power.
Syntax:
Math.pow(base, exponent);
Example:
let base = 2;
let exponent = 3;
let power = Math.pow(base, exponent);
console.log(power); // Output: 8
Math.random()
Returns a pseudorandom number between 0 (inclusive) and 1 (exclusive).
Syntax:
Math.random();
Example:
let random = Math.random();
console.log(random); // Output: A random number between 0 and 1
Math.sin(), Math.cos(), Math.tan()
These methods return the trigonometric sine, cosine, and tangent of an angle (in radians).
Syntax:
Math.sin(x);
Math.cos(x);
Math.tan(x);
Example:
let angle = Math.PI / 4; // 45 degrees in radians
let sine = Math.sin(angle);
let cosine = Math.cos(angle);
let tangent = Math.tan(angle);
console.log(sine); // Output: 0.7071 (approximately)
console.log(cosine); // Output: 0.7071 (approximately)
console.log(tangent); // Output: 1
Math.log(), Math.exp()
Math.log(x)returns the natural logarithm (basee) of a number.Math.exp(x)returnseraised to the power of a number.
Syntax:
Math.log(x);
Math.exp(x);
Example:
let value = 10;
let logarithm = Math.log(value);
let exponentiation = Math.exp(value);
console.log(logarithm); // Output: 2.3025 (approximately)
console.log(exponentiation); // Output: 22026.465 (approximately)