JavaScript Operators
JavaScript operators are the foundation of performing operations and comparisons in the language. They let you manipulate data, evaluate conditions, and build complex logic. There are four primary types of operators
- arithmetic
- comparison
- logical
- assignment.
Arithmetic Operators
Section titled “Arithmetic Operators”Arithmetic operators are used to perform mathematical calculations.
let a = 20;let b = 4;console.log(a + b); // Addition -> 24console.log(a - b); // Subtraction -> 16console.log(a * b); // Multiplication -> 80console.log(a / b); // Division -> 5console.log(a % b); // Modulus (remainder) -> 0
Comparison Operators
Section titled “Comparison Operators”Comparison operators are used to compare two values and return a boolean (true
or false
) result.
let x = 5;let y = "5";console.log(x == y); // Equal to (value only) -> true (loose equality, checks only value)console.log(x === y); // Strict equal to (value + type) -> false (strict equality, checks value and type)console.log(x != y); // Not equal to (value only) -> false (values are equal)console.log(x < 10); // Less than -> trueconsole.log(x > 1); // Greater than -> true
Always prefer using ===
and !==
over ==
and !=
to avoid unexpected type coercion issues.
coercion is the process of converting a value from one data type to another, such as converting a string to a number or a boolean to a string. Coercion can happen implicitly (automatically by JavaScript) or explicitly (manually by the developer).
Implicit Coercion
Section titled “Implicit Coercion”Occurs when JavaScript automatically converts one data type to another to execute an operation.
// String to Numberconsole.log("5" * 2); // 10 (string "5" is coerced to number 5)
// Number to Stringconsole.log("Age: " + 30); // "Age: 30" (number 30 is coerced to string "30")
// Boolean to Numberconsole.log(true + 1); // 2 (true is coerced to 1)console.log(false * 10); // 0 (false is coerced to 0)
Explicit coercion
Section titled “Explicit coercion”Occurs when a developer intentionally converts a value from one data type to another, typically using built-in global functions or operators.
// String to Numberlet str = "42";let num = Number(str); // Explicit coercionconsole.log(num); // 42 (number)
// Number to Stringlet value = 99;let text = String(value); // Explicit coercionconsole.log(text); // "99"
// Boolean to Stringlet isActive = true;console.log(String(isActive)); // "true"
// Anything to Booleanconsole.log(Boolean(0)); // false (explicit)console.log(Boolean("")); // falseconsole.log(Boolean("hello")); // true
Logical Operators
Section titled “Logical Operators”Logical operators allow you to combine multiple conditions or evaluate logical expressions.
Logical AND (&&
)
Section titled “Logical AND (&&)”Returns true
only if both conditions are true.
let a = true;let b = false;console.log(a && b); // falseconsole.log(a && !b); // true
Logical OR (||
)
Section titled “Logical OR (||)”Returns true
if one or more conditions are true.
let age = 18;console.log(age > 16 || age < 12); // trueconsole.log(age > 20 || age < 10); // false
Logical NOT (!
)
Section titled “Logical NOT (!)”Reverses a boolean value.
let isActive = true;console.log(!isActive); // false
Assignment Operators
Section titled “Assignment Operators”Assignment operators are used to assign values to variables. You can perform operations while assigning values.
let x = 10; // Assignx += 5; // Add and assign, Equivalent to x = x + 5console.log(x); // 15x *= 2; // Multiply and assign, Equivalent to x = x * 2console.log(x); // 30x -= 10; // Subtract and assign, Equivalent to x = x - 10console.log(x); // 20x /= 5; // Divide and assign, Equivalent to x = x / 5console.log(x); // 4x %= 3; // Modulus and assign , Equivalent to x = x % 3console.log(x); // 1