Java Script Operators
Java Script Operators
+ Addition 3 + 4 // 7
- Subtraction 5 - 3 // 2
* Multiplication 2 * 3 // 6
/ Division 4 / 2 // 2
% Remainder 5 % 2 // 1
++5 or 5++ //
++ Increment (increments by 1)
6
--4 or 4-- //
-- Decrement (decrements by 1)
3
** Exponentiation (Power) 4 ** 2 // 16
Example 1: Arithmetic Operators in JavaScriptlet x = 5;
// addition operator
console.log("Addition: x + 3 = ", x + 3);
// subtraction operator
console.log("Subtraction: x - 3 =", x - 3);
// multiplication operator
console.log("Multiplication: x * 3 =", x * 3);
// division operator
console.log("Division: x / 3 =", x / 3);
// remainder operator
console.log("Remainder: x % 3 =", x % 3);
// increment operator
console.log("Increment: ++x =", ++x);
// decrement operator
console.log("Decrement: --x =", --x);
// exponentiation operator
console.log("Exponentiation: x ** 3 =", x ** 3);
Output
Addition: x + 3 = 8
Subtraction: x - 3 = 2
Multiplication: x * 3 = 15
Division: x / 3 = 1.6666666666666667
Remainder: x % 3 = 2
Increment: ++x = 6
Decrement: --x = 5
Exponentiation: x ** 3 = 125
Note: The increment operator ++ adds 1 to the operand. And, the decrement
operator -- decreases the value of the operand by 1.
To learn more, visit Increment ++ and Decrement -- Operators.
2. JavaScript Assignment Operators
We use assignment operators to assign values to variables. For example,
let x = 5;
Operato
Name Example
r
= Assignment Operator a = 7;
+= Addition Assignment a += 5; // a = a + 5
-= Subtraction Assignment a -= 2; // a = a - 2
Multiplication
*= a *= 3; // a = a * 3
Assignment
/= Division Assignment a /= 2; // a = a / 2
%= Remainder Assignment a %= 2; // a = a % 2
Exponentiation
**= a **= 2; // a = a**2
Assignment
Output
Assignment: a = 7, a = 7
Addition Assignment: a += 5, a = 12
Subtraction Assignment: a -= 5, a = 7
Multiplication Assignment: a *= 2, a = 14
Division Assignment: a /= 2, a = 7
Remainder Assignment: a %= 2, a = 1
Exponentiation Assignment: a **= 7, a = 1
const a = 3, b = 2;
console.log(a > b);
// Output: true
Here, we have used the > comparison operator to check whether a (whose value
is 3) is greater than b (whose value is 2).
Since 3 is greater than 2, we get true as output.
Operato
Meaning Example
r
3 == 5 gives us
== Equal to
false
Output
Equal to: 2 == 2 is true
Not equal to: 3 != 3 is false
Strictly equal to: 2 === '2' is false
Strictly not equal to: 2 !== '2' is true
Greater than: 3 > 3 is false
Less than: 2 > 2 is false
Greater than or equal to: 3 >= 3 is true
Less than or equal to: 2 <= 2 is true
Difference between equality (== and !=) and strict equality (=== and !==) operators.
const x = 5, y = 3;
console.log((x < 6) && (y < 5));
// Output: true
Here, && is the logical operator AND. Since both x < 6 and y < 5 are true, the
combined result is true.
Commonly Used Logical Operators
true if either
|| expression1 || expression1 or
(Logical
expression2 expression2 is
OR)
true
! false if expression
(Logical !expression
is true and vice versa
NOT)
// logical AND
console.log((x < 5) && (x > 0)); // true
console.log((x < 5) && (x > 6)); // false
// logical OR
console.log((x > 2) || (x > 5)); // true
console.log((x > 3) || (x < 0)); // false
// logical NOT
console.log(!(x == 3)); // false
console.log(!(x < 2)); // true
5. JavaScript Bitwise Operators
We use bitwise operators to perform binary operations on integers.
| Bitwise OR 5 | 3 // 7
^ Bitwise XOR 5 ^ 3 // 6
~ Bitwise NOT ~5 // -6
Sign-
-10 >> 1 //
>> propagating
-5
right shift
Note: We rarely use bitwise operators in everyday programming. If you are interested,
visit JavaScript Bitwise Operators to learn more.
6. JavaScript String Concatenation Operator
In JavaScript, you can also use the + operator to concatenate (join) two
strings. For example,
let str1 = "Hel", str2 = "lo";
console.log(str1 + str2);
// Output: Hello
Comma: Evaluates
multiple operands
let a = (1,
, and returns the
3, 4); // 4
value of the last
operand.
Discards any
void(x) //
void expression's
undefined
return value.