⬅ Previous Topic
JavaScript Type Conversion - Implicit and ExplicitNext Topic ⮕
JavaScript Expressions⬅ Previous Topic
JavaScript Type Conversion - Implicit and ExplicitNext Topic ⮕
JavaScript ExpressionsOperators in JavaScript are special symbols used to perform operations on operands (values or variables). Think of them as tools that help you calculate, compare, assign, or manipulate data in your program.
These operators perform basic mathematical operations:
+
Addition-
Subtraction*
Multiplication/
Division%
Modulus (Remainder)**
Exponentiation (Power)
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);
Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3.3333333333333335 Modulus: 1 Exponentiation: 1000
These assign values to variables. You can also use them to perform operations and assign the result in one step:
=
Assign+=
Add and assign-=
Subtract and assign*=
Multiply and assign/=
Divide and assign%=
Modulus and assign
let x = 5;
x += 3; // x = x + 3
console.log("After += 3:", x);
x *= 2; // x = x * 2
console.log("After *= 2:", x);
After += 3: 8 After *= 2: 16
Used to compare two values. Returns a Boolean value (true/false):
==
Equal to (loose equality)===
Equal value and type (strict equality)!=
Not equal (loose)!==
Not equal (strict)>
Greater than<
Less than>=
Greater than or equal<=
Less than or equal
console.log(5 == "5"); // true (type coercion)
console.log(5 === "5"); // false (different types)
console.log(10 > 7); // true
console.log(3 <= 2); // false
true false true false
Used to combine multiple conditions:
&&
Logical AND||
Logical OR!
Logical NOT
let isLoggedIn = true;
let hasPermission = false;
console.log("Can access?", isLoggedIn && hasPermission);
console.log("Partial access?", isLoggedIn || hasPermission);
console.log("Not logged in?", !isLoggedIn);
Can access? false Partial access? true Not logged in? false
Works with only one operand.
+
Unary plus (converts to number)-
Unary minus (negates)++
Increment--
Decrementtypeof
Returns the data type
let y = "5";
console.log(typeof +y); // number
let count = 1;
console.log(++count); // 2
number 2
This is a shortcut for if-else
condition. Syntax: condition ? valueIfTrue : valueIfFalse
let age = 20;
let status = age >= 18 ? "Adult" : "Minor";
console.log("Status:", status);
Status: Adult
typeof
- Returns a string indicating the type of the operandinstanceof
- Checks if an object is an instance of a specific class
let text = "Hello";
console.log(typeof text); // string
let today = new Date();
console.log(today instanceof Date); // true
string true
Q: What’s the difference between ==
and ===
in JavaScript?
A: ==
compares only values (performs type coercion), while ===
compares both value and type. Always prefer ===
to avoid unexpected bugs.
Q: Why use the ternary operator when we already have if-else?
A: Ternary operator is useful for quick decisions or assignments. It makes the code concise but use it wisely to maintain readability.
⬅ Previous Topic
JavaScript Type Conversion - Implicit and ExplicitNext 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.