Arithmetic and Math Methods in JavaScript
Part 1
JavaScript Operators use either value or variable to compute some task. This lesson describes the JavaScript operators with example, and operators precedence.
Arithmetic Operators
JavaScript arithmetic operator take operand (as a values or variable) and return the single value.
+
Operator
With this operator, we can add two or more values. For example:
And our output is:
As you know, in JavaScript, a value is not only a number but also a value in string expressions. What will be the result if we want to collect them?
And our output is:
And what happens if we add the string value to the numbers? Let’s examine the result using the following code:
And our output is:
Also:
And our output is:
-
Operator
With this operator, we can find the difference of values.
Example 1:
And our output is:
Example 2:
And our output is:
But we can’t override string values here.
And our output is:
*
Operator
The multiplication operator produces the product of the operands.
And out output is:
/
Operator
The division operator produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.
Our output is:
%
Operator/Remainder Operator
The remainder
operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
Our output is:
Example 2:
Our output is:
++
Operator/Increment
Operator:
The increment operator increments adds one to its operand and returns a value. If used postfix
, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing. If used prefix
, with operator before operand (for example, ++x), the increment operator increments and returns the value after incrementing.
Postfix (x++) Increment Operation:
Our output is:
Prefix (++x) Increment Operation:
Our output is:
- -
Operator/Decrement Operator:
The decrement operator
decrements (subtracts one from) its operand and returns a value. If used postfix
, with operator after operand (for example,x- -), the decrement operator decrements and returns the value before decrementing. If used prefix
, with operator before operand (for example,- -x), the decrement operator decrements and returns the value after decrementing.
Postfix Decrement (x- -)
Our output is:
Prefix Decrement (- -x)
Our output is:
Math Methods
Math
is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object.
Math.Pi
The Math.PI
property represents the ratio of the circumference of a circle to its diameter, approximately 3.14159:
Our output is:
Math.E
The Math.E
property represents Euler's number, the base of natural logarithms, e, which is approximately 2.718.
Our output is:
Math.round()
The Math.round()
function returns the value of a number rounded to the nearest integer. If the fractional portion of the argument is greater
than 0.5, the argument is rounded to the integer with the next
higher absolute value. If it is less
than 0.5, the argument is rounded to the integer with the lower
absolute value.
Example 1
Our output is:
Example 2
Our output is:
Conclusion
And there you have it. A basic guide to arithmetic and math functions in JavaScript. We hope you found this useful!