Topics: Assignment, Comparison, Arithmetic, Bitwise, Ternary, Logical, and Unary operators
1. Assignment Operators:
Assignment operator (=), Addition assignment (+=), Subtraction assignment (-=), Multiplication assignment (*=), Division assignment (/=), Remainder assignment (%=), Exponentiation assignment (**=)
2. Comparison Operators:
Equal to (==), Not equal to (!=), Strict equal to (===), Strict not equal to (!==), Greater than >, Less than <, Greater than or equal to >=, Less than or equal to <=, Ternary operator (?:)
3. Arithmetic Operators:
Addition (+), Subtraction (-), Multiplication (*), Division (/), Remainder (%), Exponentiation (**), Increment (++) — postfix and prefix, Decrement ( — ) — postfix and prefix
4. Bitwise Operators:
Bitwise operators work on the individual bits of the operands, allowing you to manipulate and perform operations at the binary level > Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^), Bitwise NOT (~), Left shift <<, Sign-propagating right shift >>, Zero-fill right shift >>>
5. Logical Operators:
Logical operators are commonly used to combine multiple conditions and make decisions based on the truthiness or falsiness of values.
Logical AND (&&), Logical OR (||), Logical NOT (!)
Short Circuit Evaluation:
Logical AND (&&) Short Circuit: If the left operand of && is false, the right operand is not evaluated.
Logical OR (||) Short Circuit: If the left operand of || is true, the right operand is not evaluated.
let x = false && ( 10 > 5 ); // short-circuited and evaluate to false
let x = true || (10 < 5); // short-circuited and evaluate to true
Null Coalescing Operator:
let username = null; let defaultUsername = "Guest";
let greeting = "Welcome, " + (username ?? defaultUsername) + "!"; console.log(greeting); // Output: "Welcome, Guest!"
6. Ternary Operator:
syntax: condition ? expression1 : expression2
const age = 18; const isAdult = age >= 18 ? 'Yes' : 'No'; console.log(isAdult); // Output: 'Yes'
7. Unary Operators
Unary plus: +, Unary negation: -, Logical NOT: !, Increment: ++, Decrement: —
Please Check the MDN table for Operator Precedence.
1.What is the output?
let num = 8; let multiplier = 2; num *= multiplier += 3; console.log(num);
Answer: b) 40
The code snippet involves nested assignment operators. First, the multiplier is increased by 3 (multiplier += 3), resulting in multiplier having the value 5. Then, the value of num is multiplied by the updated value of multiplier (5), resulting in num having the value 40.
2. What is the output?
let a = "Hello"; let b = "hello"; let result = a < b; console.log(result);
Answer: a) true
3. What is the output?
let x = true; let y = 5; let result = x && (y > 10); console.log(result);
Answer: b) false
Code snippet uses the logical AND operator (&&) to check if both x and (y > 10) are true. Since x is true, the evaluation proceeds to (y > 10). Since y is not greater than 10 (5 > 10 is false), the overall result is false.
4. What is the Output?
let name = "John"; let fallbackName = "Anonymous"; let result = name ?? fallbackName ?? "Guest"; console.log(result);
Answer: a) “John”
This code uses multiple null coalescing operators (??) to assign the value of name to result. If name is null or undefined, it checks the next value fallbackName, and if that's also null or undefined, it finally assigns the value "Guest" to result. Since name is "John", it is assigned to result, and the value is "John".
5. What is the Output?
let x = undefined; let y; let result = x === y; console.log(result);
Answer: a) true
The code snippet compares the values of x and y using the === strict equality operator. Since both x and y are undefined, the result is true.
6.What is the Output?
let x = 5; let y = 3; x += y *= 2; console.log(x);
Answer: b) 11
First, y is multiplied by 2, resulting in y being assigned the value 6. Then, x is increased by the value of y (6) and becomes 11.
7. What is the output?
let price = 10; let discount = null; let finalPrice = price - (discount ?? 0); console.log(finalPrice);
c) 10 — discount
Answer: a) 10
The code snippet uses the null coalescing operator (??) to assign the value of discount to subtract from price. If discount is null or undefined, the value 0 is used as the discount. Since discount is null, the value of price - 0 results in 10, which is assigned to finalPrice.
8. What is the output?
let y = "Hello"; let result = y && "World"; console.log(result);
Answer: b) “World”
The code snippet uses the logical AND operator (&&) to assign the value of "World" to result. Since both y and "World" are truthy values, the expression y && "World" evaluates to the last truthy value, which is "World". Therefore, "World" is assigned to result.
9. What is the output?
let x; let result = x !== null ? x : "Default"; console.log(result);
Answer: a) undefined
The code snippet uses a conditional (ternary) operator to check if x is not null. If it's true, x is assigned to result; otherwise, the value "Default" is assigned. Since x is undefined, the condition is true, and the value "undefined" is assigned to result.