Jasyn Marais

Math Operators

12 February 2023

JavaScript provides a variety of math operators that can be used to perform arithmetic operations on numbers. Some of the most common math operators in JavaScript include:

These operators can be used in various combinations to perform more complex arithmetic operations. It's important to keep in mind the order of operations (PEMDAS) and use parentheses to control the order of operations when needed.

Math.pow

Math.pow() is a built-in JavaScript function that allows you to raise a number to a specified power. It takes two arguments: the base number and the exponent. The function returns the result of the base number raised to the power of the exponent.

Here's the basic syntax of Math.pow():

Math.pow(base, exponent);

Here's an example that demonstrates how to use Math.pow() to calculate 2 to the power of 3:

let result = Math.pow(2, 3); // 2 raised to the power of 3
console.log(result); // Output: 8

In this example, we pass the base number (2) as the first argument, and the exponent (3) as the second argument to the Math.pow() function. The function then returns the result of 2 raised to the power of 3, which is 8.

Math.round

Math.round() is a built-in JavaScript function that allows you to round a number to the nearest integer. It takes one argument, which is the number you want to round. The function returns the nearest integer to the input number.

Here's the basic syntax of Math.round():

Math.round(number);

Here's an example that demonstrates how to use Math.round() to round a number to the nearest integer:

let result = Math.round(4.7); // round 4.7 to the nearest integer
console.log(result); // Output: 5

In this example, we pass the number 4.7 as the argument to the Math.round() function. The function then rounds the number to the nearest integer, which is 5.

Math.round() can also be used to round negative numbers. For example:

let result = Math.round(-2.4); // round -2.4 to the nearest integer
console.log(result); // Output: -2

In this example, we pass the number -2.4 as the argument to the Math.round() function. The function then rounds the number to the nearest integer, which is -2.

Math.floor

Math.floor() is a built-in JavaScript function that allows you to round a number down to the nearest integer. It takes one argument, which is the number you want to round down. The function returns the largest integer less than or equal to the input number.

Here's the basic syntax of Math.floor():

Math.floor(number);

Here's an example that demonstrates how to use Math.floor() to round a number down to the nearest integer:

let result = Math.floor(4.7); // round 4.7 down to the nearest integer
console.log(result); // Output: 4

In this example, we pass the number 4.7 as the argument to the Math.floor() function. The function then rounds the number down to the nearest integer, which is 4.

Math.floor() can also be used to round negative numbers. For example:

let result = Math.floor(-2.4); // round -2.4 down to the nearest integer
console.log(result); // Output: -3

In this example, we pass the number -2.4 as the argument to the Math.floor() function. The function then rounds the number down to the nearest integer, which is -3.

Math.ceil

Math.ceil() is a built-in JavaScript function that allows you to round a number up to the nearest integer. It takes one argument, which is the number you want to round up. The function returns the smallest integer greater than or equal to the input number.

Here's the basic syntax of Math.ceil():

Math.ceil(number);

Here's an example that demonstrates how to use Math.ceil() to round a number up to the nearest integer:

let result = Math.ceil(4.7); // round 4.7 up to the nearest integer
console.log(result); // Output: 5

In this example, we pass the number 4.7 as the argument to the Math.ceil() function. The function then rounds the number up to the nearest integer, which is 5.

Math.ceil() can also be used to round negative numbers. For example:

let result = Math.ceil(-2.4); // round -2.4 up to the nearest integer
console.log(result); // Output: -2

In this example, we pass the number -2.4 as the argument to the Math.ceil() function. The function then rounds the number up to the nearest integer, which is -2.