JavaScript Operators Explained
🎯 Introduction
Operators are essential to JavaScript, whether you are performing arithmetic, checking conditions, or assigning values. Understanding them helps you write concise, efficient code with clear logic.
This article covers common arithmetic, comparison, logical, assignment, and conditional (ternary) operators, along with operator precedence and associativity. These concepts help you write more reliable code.
➕ Arithmetic Operators
Arithmetic operators perform calculations such as addition, subtraction, multiplication, and division. Common operators include:
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 3 + 4 | 7 |
| - | Subtraction | 10 - 6 | 4 |
| * | Multiplication | 5 * 8 | 40 |
| / | Division | 20 / 5 | 4 |
| % | Remainder | 9 % 4 | 1 |
| ++ | Increment (add 1) | i++ or ++i | i increases by 1 |
| -- | Decrement (subtract 1) | i-- or --i | i decreases by 1 |
Example
var a = 5;
var b = 3;
var sum = a + b; // 8
var mod = a % b; // 2
var i = 10;
i++; // i becomes 11
--i; // i becomes 10 again
🔍 Comparison Operators
Comparison operators compare two values and return a boolean (true or false). Common operators include:
| Operator | Meaning | Description |
|---|---|---|
| == | Equality | Compares values with type conversion |
| === | Strict equality | Compares values and types |
| != | Inequality | Checks inequality with type conversion |
| !== | Strict inequality | Checks for different values or types |
| > | Greater than | Left value is greater than right value |
| < | Less than | Left value is less than right value |
| >= | Greater than or equal | Left value is greater than or equal to right value |
| <= | Less than or equal | Left value is less than or equal to right value |
Example
var x = 10;
var y = '10';
console.log(x == y); // true, because type conversion occurs
console.log(x === y); // false, because the types differ
console.log(x !== y); // true
console.log(x > 5); // true
🤔 Logical Operators
Logical operators combine boolean expressions. With boolean operands, they produce the following results:
| Operator | Meaning | Description |
|---|---|---|
| && | Logical AND | Returns true only when both operands are true |
| || | Logical OR | Returns true when at least one operand is true |
| ! | Logical NOT | Reverses true to false and false to true |
Example
var a = true;
var b = false;
console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false
Logical operators are often used in conditional statements and control flow.
📝 Assignment Operators
Assignment operators assign values to variables. Besides simple assignment with =, JavaScript provides compound assignment operators:
| Operator | Meaning | Example | Description |
|---|---|---|---|
| = | Assignment | x = 10 | Assigns a value directly |
| += | Addition assignment | x += 5 | Equivalent to x = x + 5 |
| -= | Subtraction assignment | x -= 3 | Equivalent to x = x - 3 |
| *= | Multiplication assignment | x *= 2 | Equivalent to x = x * 2 |
| /= | Division assignment | x /= 4 | Equivalent to x = x / 4 |
| %= | Remainder assignment | x %= 3 | Equivalent to x = x % 3 |
Example
var x = 10;
x += 5; // x = 15
x *= 2; // x = 30
❓ The Conditional (Ternary) Operator
This is JavaScript’s only operator that takes three operands. Its syntax is:
condition ? expression1 : expression2
If the condition is true, it returns expression1; otherwise, it returns expression2.
Example
var age = 20;
var status = age >= 18 ? 'Adult' : 'Minor';
console.log(status); // Adult
The ternary operator offers a concise alternative to a simple if-else statement.
⚖️ Operator Precedence and Associativity
Operators have different precedence levels: higher-precedence operators bind more tightly. Associativity determines how operators of the same precedence group from left to right or right to left.
| Operator | Description | Precedence | Associativity |
|---|---|---|---|
| () | Parentheses | 21 | Left to right ✅ Use for clarity |
| ++, -- | Increment, decrement | 18 | Right to left |
| *, /, % | Multiplication, division, remainder | 15 | Left to right |
| +, - | Addition, subtraction | 14 | Left to right |
| <, <=, >, >= | Comparison | 12 | Left to right |
| ==, != | Equality, inequality | 11 | Left to right |
| && | Logical AND | 6 | Left to right |
| || | Logical OR | 5 | Left to right |
| =, +=, etc. | Assignment | 3 | Right to left |
| ?: | Conditional (ternary) | 4 | Right to left |
Example
var a = 2 + 3 * 4; // 3 * 4 is evaluated first; result: 14
var b = (2 + 3) * 4; // Parentheses are evaluated first; result: 20
❓ Knowledge Check
Q1: How do prefix and postfix ++ differ?
Prefix (++i) increments the variable and returns its new value. Postfix (i++) returns the original value and then increments the variable.
Q2: Why prefer === over ==?
=== does not convert types, helping avoid errors caused by implicit conversion.
Q3: How do && and || short-circuit?
With boolean operands, && skips the right operand when the left is false; || skips it when the left is true. This avoids unnecessary evaluation.
🧪 Exercises
Exercise 1: Arithmetic and Assignment
Alex has 10 units of money, earns 5 more (use +=), then loses 1 (use --). Write JavaScript to model these changes and print the remaining amount.
var money = 10;
// Write your code here
Exercise 2: Check Whether a Number Is Odd or Even
Use a ternary expression to determine whether num is odd or even and output the corresponding string.
var num = 7;
// Write your code here
alert(result); // Odd
🎉 You now understand JavaScript arithmetic, comparison, logical, assignment, and conditional operators, along with operator precedence. Keep practicing to strengthen your front-end programming skills!
