JavaScript Math Object

JavaScript's Math object is a built-in object that has properties and methods for mathematical constants and functions.

It is a static object, meaning that you do not need to instantiate it before using its properties and methods.

It can be used to perform mathematical operations like trigonometric, logarithmic, and other complex calculations.

Math Properties (Constants)

JavaScript's Math object has several properties that are mathematical constants. These properties are predefined and cannot be modified.

Here are some examples of Math properties:

  • Math.E : the base of the natural logarithm, approximately 2.718
  • Math.PI : the ratio of a circle's circumference to its diameter, approximately 3.14
  • Math.SQRT2 : the square root of 2, approximately 1.414
  • Math.SQRT1_2 : the square root of 1/2, approximately 0.707
  • Math.LN2 : the natural logarithm of 2, approximately 0.693
  • Math.LN10 : the natural logarithm of 10, approximately 2.302
  • Math.LOG2E : the base 2 logarithm of E, approximately 1.442
  • Math.LOG10E : the base 10 logarithm of E, approximately 0.434

These properties can be used in mathematical calculations and to improve the readability of the code.

For example, instead of using the value of Pi 3.14 directly in your code, you can use the Math.PI property.

Math Methods

JavaScript's Math object has several methods that can be used to perform mathematical calculations.

These methods are predefined and can be called on the Math object.

Here are some examples of Math methods:

  • Math.abs(x) : returns the absolute value of a number
  • Math.ceil(x) : rounds a number up to the nearest integer
  • Math.floor(x) : rounds a number down to the nearest integer
  • Math.round(x) : rounds a number to the nearest integer
  • Math.max(x, y, ...) : returns the largest of zero or more numbers
  • Math.min(x, y, ...) : returns the smallest of zero or more numbers
  • Math.pow(x, y) : returns the value of x to the power of y
  • Math.sqrt(x) : returns the square root of a number
  • Math.random() : returns a random number between 0 and 1
  • Math.trunc(x) : returns the integer part of a number by removing any fractional digits
  • Math.sin(x), Math.cos(x), Math.tan(x) : returns the sin, cos, tan of a number in radians.

These methods can be used to perform a variety of mathematical operations, such as rounding numbers, calculating exponents, finding the minimum or maximum of a set of numbers, and generating random numbers.

It also includes trigonometric functions like sin, cos, tan and many more.

You can use these methods by calling them on the Math object, followed by the number or numbers on which you want to perform the calculation, enclosed in parentheses.

Math.round()

Math.round() is a method of the JavaScript Math object that can be used to round a number to the nearest integer.

The method takes a single argument, which is the number that you want to round.

Here is an example of how to use the Math.round() method:

let num = 3.14;
let roundedNum = Math.round(num);
console.log(roundedNum); // Output: 3

In this example, the variable num is set to 3.14, and the Math.round() method is used to round this number to the nearest integer.

The result, 3, is then stored in the variable roundedNum and logged to the console.

It's important to note that when the decimal part of the number is exactly 0.5, the method round to the nearest even number, this is also known as "round half to even" or "bankers rounding".

let num = 2.5;
let roundedNum = Math.round(num);
console.log(roundedNum); // Output: 2

In this case, the decimal part is exactly 0.5, and since 2 is the nearest even number, the method rounds down to 2.

Math.round() can also be used on expressions or variables that evaluate to a number, and it can also be used in conjunction with other mathematical operations.

let num1 = 4.6;
let num2 = 3.2;
let sum = num1 + num2;
let roundedSum = Math.round(sum);
console.log(roundedSum); // Output: 8

In this example, the Math.round() method is used to round the sum of two numbers (num1 and num2) to the nearest integer.

It's a simple and widely used method in many situations where the exact number doesn't really matter and you want to round it to the nearest integer, for example for displaying the results in a graphical interface.

Math.ceil()

Math.ceil() is a method of the JavaScript Math object that can be used to round a number up to the nearest integer.

The method takes a single argument, which is the number that you want to round up.

Here is an example of how to use the Math.ceil() method:

let num = 3.14;
let roundedNum = Math.ceil(num);
console.log(roundedNum); // Output: 4

In this example, the variable num is set to 3.14, and the Math.ceil() method is used to round this number up to the nearest integer.

The result, 4, is then stored in the variable roundedNum and logged to the console.

It's important to note that the Math.ceil() method will always round a number up to the nearest integer.

let num = 2.5;
let roundedNum = Math.ceil(num);
console.log(roundedNum); // Output: 3

In this case, the decimal part is exactly 0.5, and since 3 is the nearest integer above 2.5, the method rounds up to 3.

Math.ceil() can also be used on expressions or variables that evaluate to a number, and it can also be used in conjunction with other mathematical operations.

let num1 = 4.1;
let num2 = 3.2;
let sum = num1 + num2;
let roundedSum = Math.ceil(sum);
console.log(roundedSum); // Output: 7

In this example, the Math.ceil() method is used to round the sum of two numbers (num1 and num2) up to the nearest integer.

It's used in situations where you want to have a number that is always rounded up, for example in financial calculations, such as banking or taxes.

It's also used when working with arrays and you want to get the size of the array to the next integer if the number of elements is not a whole number.

Math.floor()

Math.floor() is a method of the JavaScript Math object that can be used to round a number down to the nearest integer.

The method takes a single argument, which is the number that you want to round down.

Here is an example of how to use the Math.floor() method:

let num = 3.14;
let roundedNum = Math.floor(num);
console.log(roundedNum); // Output: 3

In this example, the variable num is set to 3.14, and the Math.floor() method is used to round this number down to the nearest integer.

The result, 3, is then stored in the variable roundedNum and logged to the console.

It's important to note that the Math.floor() method will always round a number down to the nearest integer.

let num = 2.5;
let roundedNum = Math.floor(num);
console.log(roundedNum); // Output: 2

In this case, the decimal part is exactly 0.5, and since 2 is the nearest integer below 2.5, the method rounds down to 2.

Math.floor() can also be used on expressions or variables that evaluate to a number, and it can also be used in conjunction with other mathematical operations.

let num1 = 4.1;
let num2 = 3.2;
let sum = num1 + num2;
let roundedSum = Math.floor(sum);
console.log(roundedSum); // Output: 7

In this example, the Math.floor() method is used to round the sum of two numbers (num1 and num2) down to the nearest integer.

It's used in situations where you want to have a number that is always rounded down, for example, when working with arrays and you want to get the size of the array to the previous integer if the number of elements is not a whole number.

It's also used in situations where you want to have a number that is always rounded down, for example, in financial calculations, such as budgeting or discounts.

Math.trunc()

Math.trunc() is a method of the JavaScript Math object that can be used to remove the decimal part of a number, effectively truncating it to an integer.

The method takes a single argument, which is the number that you want to truncate.

Here is an example of how to use the Math.trunc() method:

let num = 3.14;
let truncatedNum = Math.trunc(num);
console.log(truncatedNum); // Output: 3

In this example, the variable num is set to 3.14, and the Math.trunc() method is used to remove the decimal part of this number. The result, 3, is then stored in the variable truncatedNum and logged to the console.

It's important to note that the Math.trunc() method will always remove the decimal part of a number, whether it's positive or negative.

let num = -2.5;
let truncatedNum = Math.trunc(num);
console.log(truncatedNum); // Output: -2

In this case, the decimal part is exactly 0.5, and since -2 is the nearest integer below -2.5, the method truncates the decimal part and returns -2.

Math.trunc() can also be used on expressions or variables that evaluate to a number, and it can also be used in conjunction with other mathematical operations.

let num1 = 4.1;
let num2 = 3.2;
let sum = num1 + num2;
let truncatedSum = Math.trunc(sum);
console.log(truncatedSum); // Output: 7

In this example, the Math.trunc() method is used to remove the decimal part of the sum of two numbers (num1 and num2).

It's used in situations where you want to have a number that is always truncated, for example, when working with arrays and you want to get the size of the array to the previous integer if the number of elements is not a whole number.

It's also used in situations where you want to have a number that is always truncated, for example, in financial calculations, such as budgeting or discounts.

It's also used in situations where you want to remove the decimal part of a number that is not rounded and get the integer part of it.

Math.sign()

Math.sign() is a method of the JavaScript Math object that can be used to determine the sign of a number.

The method takes a single argument, which is the number that you want to check.

The method will return 1 if the number is positive, -1 if the number is negative, and 0 if the number is zero.

Here is an example of how to use the Math.sign() method:

let num = 3.14;
let sign = Math.sign(num);
console.log(sign); // Output: 1

In this example, the variable num is set to 3.14, and the Math.sign() method is used to determine the sign of this number.

The result, 1, is then stored in the variable sign and logged to the console.

It's important to note that the Math.sign() method will return 1 for positive numbers, -1 for negative numbers and 0 for zero.

let num = -2.5;
let sign = Math.sign(num);
console.log(sign); // Output: -1

In this case, the number is negative, the method returns -1.

let num = 0;
let sign = Math.sign(num);
console.log(sign); // Output: 0

In this case, the number is 0, the method returns 0.

Math.sign() can also be used on expressions or variables that evaluate to a number, and it can also be used in conjunction with other mathematical operations.

let num1 = 4.1;
let num2 = 3.2;
let sum = num1 + num2;
let signSum = Math.sign(sum);
console.log(signSum); // Output: 1

In this example, the Math.sign() method is used to determine the sign of the sum of two numbers (num1 and num2).

It's used in situations where you want to determine the sign of a number, for example, in mathematical or financial calculations, or in programming logic to determine if a number is positive, negative or zero.

It's also used in situations where you want to determine the direction of a value, for example, if a value is increasing or decreasing, if a number is positive or negative.

Math.pow()

Math.pow() is a method of the JavaScript Math object that can be used to raise a number to a specified power.

The method takes two arguments, the first is the number that you want to raise to a power, and the second is the power to which you want to raise the number.

Here is an example of how to use the Math.pow() method:

let num = 2;
let exponent = 3;
let result = Math.pow(num, exponent);
console.log(result); // Output: 8

In this example, the variable num is set to 2 and the variable exponent is set to 3, the Math.pow() method is used to raise the number 2 to the power of 3.

The result, 8, is then stored in the variable result and logged to the console.

It's important to note that the Math.pow() method will return the result of num raised to the power of exponent.

let num = -2;
let exponent = 2;
let result = Math.pow(num, exponent);
console.log(result); // Output: 4

In this case, the number is -2 and the exponent is 2, the method returns 4.

Math.pow() can also be used on expressions or variables that evaluate to a number, and it can also be used in conjunction with other mathematical operations.

let num1 = 2;
let num2 = 3;
let multiple = num1 * num2;
let result = Math.pow(multiple, 2);
console.log(result); // Output: 36

In this example, the Math.pow() method is used to raise the product of two numbers (num1 and num2) to the power of 2.

It's used in situations where you want to raise a number to a specific power, for example, in mathematical or scientific calculations, or in programming logic to calculate the exponent of a number.

It's also used in situations where you want to calculate the square or cube of a number.

It's important to note that if the first argument is negative and the second argument is not an integer, the method will return NaN (Not a Number).

Also, if the first argument is zero and the second argument is less than or equal to zero, the method will return NaN.

JS Basics