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:
+
(addition): This operator is used to add two numbers
together. For example, 3 + 5 would result in the value 8.
-
(subtraction): This operator is used to subtract one
number from another. For example, 10 - 3 would result in the value 7.
*
(multiplication): This operator is used to multiply two
numbers together. For example, 2 * 4 would result in the value 8.
/
(division): This operator is used to divide one number by
another. For example, 10 / 2 would result in the value 5.
%
(modulus): This operator is used to get the remainder of
a division operation. For example, 10 % 3 would result in the value 1.
++
(increment): This operator is used to add one to a
variable's value. For example, if x is equal to 5, then x++ would result
in 6.
--
(decrement): This operator is used to subtract one from
a variable's value. For example, if y is equal to 8, then y-- would
result in 7.
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()
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()
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() 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() 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.