Explain JavaScript Operators
Sure! JavaScript operators are symbols used to perform operations on operands (values and variables). They can be categorized into several types:
1. Arithmetic Operators
These operators perform basic arithmetic operations.
-
Addition (+): Adds two numbers.
let a = 5; let b = 3; console.log(a + b); // Output: 8 -
Subtraction (-): Subtracts the second number from the first.
console.log(a - b); // Output: 2 -
Multiplication (*): Multiplies two numbers.
console.log(a * b); // Output: 15 -
Division (/): Divides the first number by the second.
console.log(a / b); // Output: 1.6666666666666667 -
Modulus (%): Returns the remainder of the division.
console.log(a % b); // Output: 2 -
Exponentiation ()**: Raises the first number to the power of the second.
console.log(a ** b); // Output: 125
2. Assignment Operators
These operators assign values to variables.
-
Assignment (=): Assigns the value on the right to the variable on the left.
let c = 10; -
Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
c += 5; // Equivalent to c = c + 5 console.log(c); // Output: 15 -
Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
c -= 3; // Equivalent to c = c - 3 console.log(c); // Output: 12 -
Multiplication Assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
c *= 2; // Equivalent to c = c * 2 console.log(c); // Output: 24 -
Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
c /= 4; // Equivalent to c = c / 4 console.log(c); // Output: 6
3. Comparison Operators
These operators compare two values and return a boolean value.
-
Equal (==): Checks if two values are equal.
console.log(a == 5); // Output: true -
Not Equal (!=): Checks if two values are not equal.
console.log(a != 5); // Output: false -
Strict Equal (===): Checks if two values are equal and of the same type.
console.log(a === 5); // Output: true -
Strict Not Equal (!==): Checks if two values are not equal or not of the same type.
console.log(a !== "5"); // Output: true -
Greater Than (>): Checks if the left value is greater than the right value.
console.log(a > b); // Output: true -
Less Than (<): Checks if the left value is less than the right value.
console.log(a < b); // Output: false -
Greater Than or Equal (>=): Checks if the left value is greater than or equal to the right value.
console.log(a >= b); // Output: true -
Less Than or Equal (<=): Checks if the left value is less than or equal to the right value.
console.log(a <= b); // Output: false
4. Logical Operators
These operators perform logical operations.
-
AND (&&): Returns true if both operands are true.
console.log(true && false); // Output: false -
OR (||): Returns true if at least one of the operands is true.
console.log(true || false); // Output: true -
NOT (!): Inverts the boolean value.
console.log(!true); // Output: false
5. Bitwise Operators
These operators perform bit-level operations.
-
AND (&): Performs a bitwise AND.
console.log(a & b); // Output: 1 (binary: 0101 & 0011 = 0001) -
OR (|): Performs a bitwise OR.
console.log(a | b); // Output: 7 (binary: 0101 | 0011 = 0111) -
XOR (^): Performs a bitwise XOR.
console.log(a ^ b); // Output: 6 (binary: 0101 ^ 0011 = 0110) -
NOT (~): Performs a bitwise NOT.
console.log(~a); // Output: -6 (binary: ~0101 = 1010) -
Left Shift (<<): Shifts bits to the left.
console.log(a << 1); // Output: 10 (binary: 0101 << 1 = 1010) -
Right Shift (>>): Shifts bits to the right.
console.log(a >> 1); // Output: 2 (binary: 0101 >> 1 = 0010)
6. Ternary Operator
The ternary operator is a shorthand for an if-else statement.
- Conditional (?:): Returns a value based on a condition.
let isAdult = (a > 18) ? "Yes" : "No"; console.log(isAdult); // Output: No
7. Type Operators
These operators determine the type of a variable.
-
typeof: Returns the type of a variable.
console.log(typeof a); // Output: number -
instanceof: Checks if an object is an instance of a specific class.
let date = new Date(); console.log(date instanceof Date); // Output: true
These are the basic JavaScript operators with examples to illustrate their usage.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
