Operators in JavaScript allow you to perform operations on data such as calculations, comparisons, logical checks, and assignments. They are a fundamental part of working with variables and values, enabling you to write dynamic, readable, and functional code.
Arithmetic Operators in JavaScript
Arithmetic operators are used to perform basic mathematical operations on numeric values.
List of JavaScript Arithmetic Operators
| Operator | Description | Example | Output |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 3 | 18 |
/ | Division | 15 / 3 | 5 |
% | Modulus (Remainder) | 17 % 5 | 2 |
++ | Increment | let x = 5; x++ | 6 |
-- | Decrement | let y = 5; y-- | 4 |
Arithmetic Operator Examples
let x = 10;
let y = 3;
console.log(x + y); // Output: 13
console.log(x - y); // Output: 7
console.log(x * y); // Output: 30
console.log(x / y); // Output: 3.3333333333333335
console.log(x % y); // Output: 1
console.log(x ** y); // Output: 1000
Increment and Decrement Behavior
x++orx--: value changes after evaluation++xor--x: value changes before evaluation
Comparison Operators in JavaScript
Comparison operators compare two values and return a Boolean value (true or false). They are commonly used in conditional logic.
JavaScript Comparison Operator Reference
| Operator | Description | Example | Output |
|---|---|---|---|
== | Equal to | 5 == '5' | true |
=== | Strict equal (value + type) | 5 === '5' | false |
!= | Not equal | 5 != '6' | true |
!== | Strict not equal | 5 !== '5' | true |
> | Greater than | 10 > 5 | true |
< | Less than | 3 < 7 | true |
>= | Greater than or equal | 8 >= 8 | true |
<= | Less than or equal | 4 <= 3 | false |
Comparison Operator Examples
let a = 5;
let b = '5';
console.log(a == b); // true (loose equality)
console.log(a === b); // false (strict equality)
console.log(a != b); // false
console.log(a !== b); // true
console.log(a > 3); // true
console.log(a < 10); // true
console.log(a >= 5); // true
console.log(a <= 4); // false
Difference Between == and ===
==compares values only===compares both value and type (recommended)
Logical Operators in JavaScript
Logical operators are used to combine or invert Boolean values and expressions.
JavaScript Logical Operators
| Operator | Description | Example | Output |
|---|---|---|---|
&& | Logical AND | true && false | false |
| ` | ` | Logical OR | |
! | Logical NOT | !true | false |
Logical Operator Examples
let isAdult = true;
let hasPermission = false;
let canEnter = isAdult && hasPermission; // false
let canView = isAdult || hasPermission; // true
let isDenied = !hasPermission; // true
Logical Operator Behavior
&&→ true only if both conditions are true||→ true if at least one condition is true!→ reverses the Boolean value
Assignment Operators in JavaScript
Assignment operators assign values to variables and include shorthand forms for updating values.
JavaScript Assignment Operators
| Operator | Description | Example | Equivalent |
|---|---|---|---|
= | Assignment | x = 10 | – |
+= | Addition assignment | x += 5 | x = x + 5 |
-= | Subtraction assignment | x -= 3 | x = x - 3 |
*= | Multiplication assignment | x *= 2 | x = x * 2 |
/= | Division assignment | x /= 4 | x = x / 4 |
%= | Modulus assignment | x %= 2 | x = x % 2 |
Assignment Operator Examples
let x = 10;
x += 5;
console.log(x); // 15
x -= 3;
console.log(x); // 12
x *= 2;
console.log(x); // 24
x /= 4;
console.log(x); // 6
x %= 3;
console.log(x); // 0
x = 2;
x **= 3;
console.log(x); // 8
Conditional (Ternary) Operator in JavaScript
The ternary operator provides a concise alternative to an if-else statement.
Ternary Operator Syntax
let result = condition ? valueIfTrue : valueIfFalse;
Basic Ternary Operator Example
let age = 20;
let access = age >= 18 ? "Allowed" : "Denied";
console.log(access); // Output: "Allowed"
Nested Ternary Operator Example
let score = 85;
let grade = score >= 90 ? 'A' : score >= 80 ? 'B' : 'C';
console.log(grade); // Output: "B"
⚠️ Best Practice:
Avoid excessive nesting of ternary operators as it reduces readability. Use if-else when logic becomes complex.
Summary of JavaScript Operators
JavaScript provides a rich set of operators to handle calculations, comparisons, logic, and assignments:
- Arithmetic operators perform mathematical operations
- Comparison operators evaluate conditions
- Logical operators combine or invert Boolean expressions
- Assignment operators simplify value updates
- Ternary operator offers a concise conditional syntax
Understanding and using these operators effectively is essential for writing clear, expressive, and efficient JavaScript code.
Leave a Reply