Operators are represented by symbols or keywords that perform specific operations on values in JavaScript. They are used to manipulate data, create expressions, and control the flow of your code.
There are 5 common operators in JavaScript.
Arithmetic operators is used to perform mathematical operations such as addition, subtraction, multiplication, division, modulo, increment, decrement.
Syntax:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulo)
++ (increment)
-- (decrement)
let addition = 5 + 3; // 8
let subtraction = 5 - 3; // 2
let multiplication = 5 * 3; // 15
let division = 5 / 3; // 1
let remainder = 5 % 3; // 2
++addition; // pre-increment
addition++; // post-increment
--addition; // pre-decrement
addition--; // post-decrement
Comparison operators is used to compare values and return a boolean result. comparison between values done such as equal to, not equal to, strictly equal to, strictly not equal to, greater than, less than, greater than or equal to, less than or equal to.
Syntax:
== (equal to)
!= (not equal to)
=== (strictly equal to)
!== (strictly not equal to)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
let x = 10;
let y = 5;
if (x > y) {
console.log("x is greater than y");
}
Logical operators is used to combine boolean values such as logical AND, logical OR, logical NOT.
Syntax:
&& (logical AND)
|| (logical OR)
! (logical NOT)
let isSunny = true;
let isWeekend = true;
if (isSunny && isWeekend) {
console.log("It's a party day!");
}
Assignment operators is used to assign values to variables such as assignment, addition assignment, subtraction assignment, multiplication assignment, division assignment, modulo assignment.
Syntax:
= (assignment)
+= (addition assignment)
-= (subtraction assignment)
*= (multiplication assignment)
/= (division assignment)
%= (modulo assignment)
let salary = 2500;
salary += 500; // salary with increment of 500
Ternary operators is used for a shorthand way to write an if-else statement.
Syntax: condition ? expression1 : expression2
let isAdult = age >= 18 ? "Adult" : "Minor";
- Arithmetic Operators:
- For mathematical calculations, data manipulation, and creating expressions.
- Comparison Operators:
- For making decisions based on conditions and controlling the flow of your code.
- Logical Operators:
- For combining boolean values and creating complex conditions.
- Assignment Operators:
- For assigning values to variables and updating their values.
- Ternary Operator:
- For concisely writing conditional expressions.