Operators play a pivotal role in programming, enabling developers to perform various operations on data. From simple arithmetic calculations to complex logical evaluations, understanding how to effectively use operators is crucial for writing efficient and readable code. In this blog post, we’ll dive into the different types of operators – arithmetic, comparison, logical, assignment, bitwise, unary, and the ternary operator – with examples, tips, and snippets to enhance your coding skills.
Arithmetic Operators
Arithmetic operators are used for basic mathematical operations.
let a = 10;
let b = 5;
// Addition
console.log(a + b); // 15
// Subtraction
console.log(a – b); // 5
// Multiplication
console.log(a * b); // 50
// Division
console.log(a / b); // 2
// Modulus (Remainder)
console.log(a % b); // 0
// Increment
a++;
console.log(a); // 11
// Decrement
b–;
console.log(b); // 4
Tip: Use ++ and — for efficient, concise incrementing/decrementing of values.
Comparison Operators
Comparison operators compare two values and return a boolean result.
let x = 10;
let y = “10”;
// Equal to
console.log(x == y); // true
// Strictly equal to (value and type)
console.log(x === y); // false
// Not equal to
console.log(x != y); // false
// Greater than
console.log(x > 8); // true
// Less than
console.log(x < 12); // true
Tip: Always use === and !== for comparison to avoid unexpected type coercion.
Logical Operators
Logical operators are used with boolean (logical) values.
let a = true;
let b = false;
// Logical AND
console.log(a && b); // false
// Logical OR
console.log(a || b); // true
// Logical NOT
console.log(!a); // false
Tip: Logical operators can simplify your if-statements and conditional logic.
Assignment Operators
Assignment operators assign values to variables.
let a = 10;
// Simple assignment
a = 5;
// Addition assignment
a += 5; // a = a + 5
// Subtraction assignment
a -= 2; // a = a – 2
// Multiplication assignment
a *= 2; // a = a * 2
// Division assignment
a /= 4; // a = a / 4
Tip: Use compound assignment operators for cleaner and more concise code.
Bitwise Operators
Bitwise operators perform operations on binary representations of numbers.
let a = 5; // 0101 in binary
let b = 3; // 0011 in binary
// AND
console.log(a & b); // 1 (0001)
// OR
console.log(a | b); // 7 (0111)
// XOR
console.log(a ^ b); // 6 (0110)
// NOT
console.log(~a); // -6 (in 32 bits)
Tip: Bitwise operators are powerful for low-level programming tasks and optimizations.
Unary Operators
Unary operators work with a single operand.
let a = 5;
// Unary plus (converts operand to a number)
console.log(+a); // 5
// Unary negation
console.log(-a); // -5
Tip: Use unary plus to ensure a value is treated as a number, useful for user input that’s always a string.
Ternary Operator
The ternary operator is a shorthand for the if-else statement.
let age = 19;
// Condition ? ExprIfTrue : ExprIfFalse
let canVote = age >= 18 ? “Yes” : “No”;
console.log(canVote); // “Yes”
Tip: The ternary operator is great for simple conditions but avoid using it for complex logic as it can reduce readability.