⬅ Previous Topic
JavaScript Type ConversionNext Topic ⮕
JavaScript Expressions⬅ Previous Topic
JavaScript Type ConversionNext Topic ⮕
JavaScript ExpressionsJavaScript operators allow you to perform operations on variables and values. From basic math to complex logic evaluation, operators play a central role in making decisions, transforming data, and controlling program flow.
Used to perform basic math operations.
let a = 10;
let b = 3;
console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);
console.log("Modulus:", a % b);
console.log("Exponentiation:", a ** b);
console.log("Increment a:", ++a);
console.log("Decrement b:", --b);
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus: 1
Exponentiation: 1000
Increment a: 11
Decrement b: 2
Used to assign values to variables. Some also perform operations.
let x = 5;
x += 3; // Same as x = x + 3
x -= 2;
x *= 2;
x /= 3;
x %= 2;
Final value of x: 2
Compare two values and return a boolean (true/false).
console.log(10 == '10'); // Loose equality (true)
console.log(10 === '10'); // Strict equality (false)
console.log(5 != 7); // true
console.log(5 !== '5'); // true
console.log(5 > 3); // true
console.log(5 >= 5); // true
console.log(2 < 4); // true
console.log(2 <= 1); // false
true
false
true
true
true
true
true
false
Used for combining multiple conditions or expressions.
let a = true;
let b = false;
console.log(a && b); // AND
console.log(a || b); // OR
console.log(!a); // NOT
false
true
false
Operate on binary representations of numbers.
let x = 5; // 0101
let y = 3; // 0011
console.log(x & y); // AND -> 0001 => 1
console.log(x | y); // OR -> 0111 => 7
console.log(x ^ y); // XOR -> 0110 => 6
console.log(~x); // NOT -> 1010 => -6 (2's complement)
console.log(x << 1); // Left shift => 1010 => 10
console.log(x >> 1); // Right shift => 0010 => 2
1
7
6
-6
10
2
Used to check the type of a variable or to determine if an object is an instance of a class.
let name = "Alice";
let age = 30;
console.log(typeof name); // string
console.log(typeof age); // number
class Person {}
let p = new Person();
console.log(p instanceof Person); // true
string
number
true
A shorthand for if...else
conditions.
let score = 85;
let result = (score >= 60) ? "Pass" : "Fail";
console.log(result);
Pass
Returns the right-hand operand when the left is null
or undefined
.
let userName = null;
let displayName = userName ?? "Guest";
console.log(displayName);
Guest
Safely access deeply nested properties without errors if intermediate is null or undefined.
let user = {
profile: {
name: "John"
}
};
console.log(user.profile?.name); // John
console.log(user.settings?.theme); // undefined (no error)
John
undefined
Understanding JavaScript operators helps you control your code with precision and logic. Whether you're evaluating expressions, comparing values, or chaining safe access, these operators empower you to write efficient and readable code. Practice is key — try modifying the examples and observe how changes affect the output.
⬅ Previous Topic
JavaScript Type ConversionNext Topic ⮕
JavaScript ExpressionsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.