Performing Maths Operation in JavaScript

by | Dec 25, 2020 | JavaScript

Home » Website Development » JavaScript » Performing Maths Operation in JavaScript

Mathematical Operators in JavaScript

In this section, we will explore all the mathematical operators and mathematical functions available in JavaScript.

To perform mathematical operation, we will need all the mathematics operators, that we know. It can be divided into given five sections:

1. JavaScript Arithmetic Operators

These operators are used to perform Arithmetic operations of numbers.

OperatorDescriptionExample
+Addition4 + 2 = 6
Subtraction4 – 2 = 2
*Multiplication4 * 2 = 8
/Division4 / 2 = 2
%Modulus (Division Remainder)4 % 2 = 0
++Increment4++ = 5
Decrement4– = 3

 

2. JavaScript Assignment Operators

These operators are used to assign values to a variable in JavaScript.

OperatorDescriptionExample
=a = ba = 3
+=a += b à a = a + ba += 3 à a = a + 3
-=a -= b  à a = a – ba -= 3 à a = a – 3
*=a *= b à a = a* ba *= 3 à a = a * 3
/=a /= b à a = a / ba /= 3 à a = a / 3
%=a%=b àa = a % ba %= 3 à a = a % 3

 

3. JavaScript Comparison Operators

These operators are used to perform basic comparisons between any two numbers. In given examples, we assume that value of X = 4.

OperatorDescriptionExampleResult
==Equal toX == 5

X == 4

X == “4”

False

True

True

===Equal value and equal typeX === 4

X === “4”

Ture

False

!=Not equalX != 5True
!==Not equal value or not equal typeX !== 4

X !== “4”

X !== 5

False

True

True

>Greater thanX > 5False
<Less thanX < 5True
>=Greater than or Equal toX >= 5False
<=Less than or Equal toX <= 5True
?Ternary Operator

Syntax:

condition ? exprT : exprF

? stands for If True than

: stands for If False than

var age = 25;

var beverage = (age >= 21) ? “Beer” : “Juice”;

console.log(beverage);

Beer

 

 

4. JavaScript Logical Operators

Logical operators are used to determine the logic between two variables, values or conditions. In some cases, we need to add two or more comparisons for same variables and in that case, we use JavaScript logical operators.

In given examples we have assumed the value of x = 2 and y = 4.

OperatorDescriptionExampleResult
&&and(x < 5 && y > 1)True
||or(x == 4 || y == 2)False
!not!(x == y)True

 

5. JavaScript Bitwise Operators

The bitwise operators are very similar to the JavaScript Logical operators, except they work on a smaller scale i.e. binary representation of data.

Following are the operators with their functionality:

OperatorFunctionality
op1 & op2It compares two bits and generates a result of 1 if both bits are 1, else it returns 0.
op1 | op2It compares two bits and generates a result of 1 if bits are complementary, else 0.
op1^ op2It compares two bits and generates a result of 1 if only either of the bits are 1, else 0.
~op1It inverts all the bits of the operand (the number).
op1 >> op2It moves the bits to the right, discards the far-right bit, and assigns the leftmost bit a value of 0.
op1 << op2It moves the bits to the left, discards the far-left bit, and assigns the rightmost bit a value of 0.

Note:  Both operands associated with the bitwise operator must be integers. All the bits, of an integer, are compared individually.

 

OperatorDescriptionExampleBitwise RepresentationResultDecimal
&AND5 & 10101 & 000100011
|OR5 | 10101 | 000101015
~NOT~ 5~0101101010
^XOR5 ^ 10101 ^ 000101004
<<Zero fill left shift5 << 10101 << 1101010
>>Signed right shift5 >> 10101 >> 100102
>>>Zero fill right shift5 >>> 10101 >>> 100102

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author