Author
Ques: What are Operators in JavaScript?
Ans: Operators are symbols used to perform operations on values and variables, such as addition, comparison, or logic.
Example:
let a = 10, b = 5;
console.log(a + b); // 15
Ques: What are the different types of operators in JavaScript?
Ans: JavaScript provides several types of operators:
| Type | Example | Description |
|---|---|---|
| Arithmetic | + - * / % ** |
Perform math operations |
| Assignment | = += -= *= /= |
Assign values |
| Comparison | == === != !== > < >= <= |
Compare values |
| Logical | `&& | |
| Bitwise | `& | ^ ~ << >> >>>` |
| Ternary | ? : |
Conditional check |
| Type | typeof, instanceof |
Check data types |
| String | +, += |
Concatenate strings |
Ques: What are arithmetic operators?
Ans: These are used to perform mathematical calculations.
| Operator | Description | Example | Output |
|---|---|---|---|
+ |
Addition | 5 + 2 |
7 |
- |
Subtraction | 5 - 2 |
3 |
* |
Multiplication | 5 * 2 |
10 |
/ |
Division | 10 / 2 |
5 |
% |
Modulus (Remainder) | 10 % 3 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
Example:
let x = 10;
x++;
console.log(x); // 11
x--;
console.log(x); // 10
Ques: What are assignment operators?
Ans: They are used to assign or update values to variables.
| Operator | Example | Equivalent To |
|---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 2 |
x = x * 2 |
/= |
x /= 2 |
x = x / 2 |
%= |
x %= 2 |
x = x % 2 |
Example:
let num = 10;
num += 5;
console.log(num); // 15
Ques: What are comparison operators used for?
Ans: Comparison operators are used to compare two values and return a boolean (true or false).
| Operator | Description | Example | Output |
|---|---|---|---|
== |
Equal to | 5 == '5' |
true |
=== |
Strict equal to | 5 === '5' |
false |
!= |
Not equal to | 5 != '5' |
false |
!== |
Strict not equal to | 5 !== '5' |
true |
> |
Greater than | 6 > 3 |
true |
< |
Less than | 2 < 5 |
true |
>= |
Greater than or equal | 5 >= 5 |
true |
<= |
Less than or equal | 4 <= 6 |
true |
Ques: What are logical operators?
Ans: Logical operators combine or invert conditions and return a boolean (true / false).
| Operator | Description | Example | Result |
|---|---|---|---|
&& |
Logical AND | (5 > 2 && 6 > 3) |
true |
| ` | ` | Logical OR | |
! |
Logical NOT | !(5 > 2) |
false |
Example:
let age = 25;
if (age >= 18 && age <= 30) {
console.log("Eligible");
}
Ques: What are bitwise operators?
Ans: These perform operations at the binary (bit) level.
| Operator | Description | Example | Result |
|---|---|---|---|
& |
AND | 5 & 1 |
1 |
| ` | ` | OR | `5 |
^ |
XOR | 5 ^ 1 |
4 |
~ |
NOT | ~5 |
-6 |
<< |
Left shift | 5 << 1 |
10 |
>> |
Right shift | 5 >> 1 |
2 |
Ques: What is the ternary operator?
Ans: The ternary operator is a shortcut for if...else statements.
Syntax:
condition ? value_if_true : value_if_false;
Example:
let age = 18;
let result = (age >= 18) ? "Adult" : "Minor";
console.log(result); // Adult
Ques: What are type operators?
Ans: Used to check data types or relationships of objects.
| Operator | Description | Example | Output |
|---|---|---|---|
typeof |
Checks data type | typeof 42 |
"number" |
instanceof |
Checks instance | arr instanceof Array |
true |
Example:
console.log(typeof "Hello"); // string
console.log(typeof null); // object
console.log([1, 2] instanceof Array); // true
Ques: What are string operators?
Ans: The + and += operators are used to concatenate strings.
Example:
let str1 = "Hello";
let str2 = "World";
console.log(str1 + " " + str2); // "Hello World"
Ques: What is operator precedence?
Ans: It defines the order in which operations are performed.
Example:
let result = 10 + 5 * 2;
console.log(result); // 20 (Multiplication before Addition)
Ques: What’s the difference between == and ===?
== → Compares values only (performs type conversion).=== → Compares value and data type (strict).Example:
0 == false; // true
0 === false; // false
Ques: What will "5" + 2 and "5" - 2 return?
"5" + 2; // "52" (string concatenation)
"5" - 2; // 3 (string converted to number)
Ques: What is typeof null?
Ans: Returns "object" — this is a known JavaScript bug.
Ques: What is operator associativity?
Ans: It defines direction of execution —
+, , , /)=, *)