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.

Operator Description Example
+ Addition 4 + 2 = 6
Subtraction 4 – 2 = 2
* Multiplication 4 * 2 = 8
/ Division 4 / 2 = 2
% Modulus (Division Remainder) 4 % 2 = 0
++ Increment 4++ = 5
Decrement 4– = 3

 

2. JavaScript Assignment Operators

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

Operator Description Example
= a = b a = 3
+= a += b à a = a + b a += 3 à a = a + 3
-= a -= b  à a = a – b a -= 3 à a = a – 3
*= a *= b à a = a* b a *= 3 à a = a * 3
/= a /= b à a = a / b a /= 3 à a = a / 3
%= a%=b àa = a % b a %= 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.

Operator Description Example Result
== Equal to X == 5

X == 4

X == “4”

False

True

True

=== Equal value and equal type X === 4

X === “4”

Ture

False

!= Not equal X != 5 True
!== Not equal value or not equal type X !== 4

X !== “4”

X !== 5

False

True

True

> Greater than X > 5 False
< Less than X < 5 True
>= Greater than or Equal to X >= 5 False
<= Less than or Equal to X <= 5 True
? 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.

Operator Description Example Result
&& 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:

Operator Functionality
op1 & op2 It compares two bits and generates a result of 1 if both bits are 1, else it returns 0.
op1 | op2 It compares two bits and generates a result of 1 if bits are complementary, else 0.
op1^ op2 It compares two bits and generates a result of 1 if only either of the bits are 1, else 0.
~op1 It inverts all the bits of the operand (the number).
op1 >> op2 It moves the bits to the right, discards the far-right bit, and assigns the leftmost bit a value of 0.
op1 << op2 It 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.

 

Operator Description Example Bitwise Representation Result Decimal
& AND 5 & 1 0101 & 0001 0001 1
| OR 5 | 1 0101 | 0001 0101 5
~ NOT ~ 5 ~0101 1010 10
^ XOR 5 ^ 1 0101 ^ 0001 0100 4
<< Zero fill left shift 5 << 1 0101 << 1 1010 10
>> Signed right shift 5 >> 1 0101 >> 1 0010 2
>>> Zero fill right shift 5 >>> 1 0101 >>> 1 0010 2

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.


Comments

Leave a Reply

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.