JavaScript Operators Flashcards

possible operators in JS (45 cards)

1
Q

What is the difference between == and === in JavaScript?

A

== (Loose Equality): Compares values after performing type coercion (attempts to convert the values to a common type). Avoid using this unless you know exactly what you’re doing.

=== (Strict Equality): Compares values without type coercion. Values must be of the same type and have the same value to return true. Use this one almost always.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give an example where == returns true but === returns false.

A

console.log(1 == “1”); // true (Type coercion: string “1” to number 1)
console.log(1 === “1”); // false (Different types: number and string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are != and !==? How do they differ?

A

!= (Loose Inequality): Returns true if the operands are not equal after type coercion. Avoid this.

!== (Strict Inequality): Returns true if the operands are not equal or are of different types. Prefer this one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is “type coercion”? Why is it important to understand?

A

Type coercion is JavaScript’s automatic conversion of values from one data type to another (e.g., string to number) during certain operations. It’s important because it can lead to unexpected and incorrect results if you’re not aware of it, especially when using == and !=.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain how strings are compared using <, >, <=, and >=.

A

Strings are compared lexicographically (dictionary order) based on the Unicode values of their characters. The comparison proceeds character by character from left to right.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Is “apple” < “Banana” true or false? Why?

A

false. Uppercase letters have lower Unicode values than lowercase letters. Therefore, “B” < “a”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you perform a case-insensitive string comparison in JavaScript?

A

Convert both strings to lowercase (using .toLowerCase()) or uppercase (using .toUpperCase()) before comparing them.

let str1 = “Apple”.toLowerCase();
let str2 = “apple”.toLowerCase();
console.log(str1 === str2); // true (case-insensitive comparison)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens when you compare a string and a number using <, >, <=, or >=?

A

JavaScript usually attempts to convert the string to a number. If the string can be converted to a valid number, the comparison is performed numerically. If the string cannot be converted (resulting in NaN), the comparison usually returns false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why should you generally avoid using == in JavaScript?

A

Because its type coercion can lead to unexpected and difficult-to-debug behavior. === provides more predictable and reliable comparisons.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the result of null == undefined and null === undefined?

A

null == undefined is true (a special case in loose equality).

null === undefined is false (they are different types).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain the pitfall of comparing objects (like {} or []) using == or ===.

A

== and === only check if the objects refer to the same object in memory (object identity), not whether they have the same properties and values (structural equality). {} == {} is false. [] == [] is false. You need to compare object properties individually to check for structural equality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will the following code output?
console.log(“2” > “12”);

A

true. String comparison is lexicographical. “2” comes after “1” in dictionary order, so ‘2’ > ‘12’ is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you numerically compare strings that contain numbers?

A

Convert them to numbers first using Number(), parseInt(), or parseFloat().

console.log(Number(“10”) < Number(“2”)); // false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the three JavaScript logical operators?

A

&& (AND), || (OR), ! (NOT)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the purpose of the && (AND) operator. What must be true for A && B to evaluate to true?

A

The && operator returns true if both operands are true. It returns false otherwise. Both A and B must be true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain the purpose of the || (OR) operator. What must be true for A || B to evaluate to true?

A

The || operator returns true if at least one of the operands is true. It returns false only if both operands are false. At least one of A or B must be true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Explain the purpose of the ! (NOT) operator. What does it do to a boolean value?

A

The ! operator inverts the boolean value of its operand. !true is false, and !false is true. It negates the truthiness/falsiness of the value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is “short-circuit evaluation” and how does it apply to the && (AND) operator?

A

Short-circuit evaluation means that the second operand of && is only evaluated if the first operand is true. If the first operand is false, the result is immediately false, and the second is skipped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is “short-circuit evaluation” and how does it apply to the || (OR) operator?

A

Short-circuit evaluation means that the second operand of || is only evaluated if the first operand is false. If the first operand is true, the result is immediately true, and the second is skipped.

20
Q

What is the order of precedence for logical operators?

A

! (NOT) has the highest precedence, followed by && (AND), and then || (OR). Use parentheses () to override the default order.

21
Q

What are some common use cases for logical operators?

A

Validating Input
Controlling Program Flow
Access Control
Error Handling
Creating complex conditional expressions

22
Q

What are “truthy” and “falsy” values? Give some examples of falsy values.

A

“Truthy” values are values that are implicitly converted to true when used in a boolean context. “Falsy” values are: false, 0, “” (empty string), null, undefined, NaN.

23
Q

Why is readability important when working with logical operators? How can you improve the readability of your code when using logical operators?

A

Use parentheses to make evaluation order explicit.
Use descriptive variable names.
Break down complex conditions.
Add comments.
Avoid overly complex expressions.

24
Q

Give an example of using logical operators to validate if a number is within a specific range.

A

let number = 55;
let min = 10;
let max = 100;

if (number >= min && number <= max) {
console.log(“The number is within the range.”);
} else {
console.log(“The number is outside the range.”);
}

25
How can short-circuit evaluation be helpful in avoiding errors? Give an example.
Short-circuiting prevents errors by avoiding risky operations unless conditions are safe. let x = 10; let y = 0; if (y !== 0 && x / y > 2) { console.log("The result is: " + x / y); } else { console.log("Cannot perform division."); }
26
What is the Ternary Operator in JavaScript?
A concise way to write a conditional expression. Shorthand for a simple if-else statement. Syntax: condition ? valueIfTrue : valueIfFalse
27
Explain the Syntax of the Ternary Operator.
condition: An expression that evaluates to true or false. ?: The ternary operator symbol. valueIfTrue: The value returned if the condition is true. :: Separates the valueIfTrue and valueIfFalse. valueIfFalse: The value returned if the condition is false.
28
Give an example of a Ternary Operator in Action.
Generated javascript let age = 20; let isAdult = age >= 18 ? true : false; // isAdult will be true console.log(isAdult); // Output: true
29
What is Inline Conditional Assignment, and how does the Ternary Operator help with it?
Assigning a value to a variable based on a condition. Ternary operators provide a concise way to achieve this in a single line. Generated javascript let isLoggedIn = true; let userStatus = isLoggedIn ? "Online" : "Offline";
30
How is the Ternary Operator used in Conditional Rendering (e.g., in React)?
To conditionally render different elements or components based on a condition. Generated jsx {isLoggedIn ?

Welcome!

:

Please log in.

}
31
What are Chained Ternary Operations? Why are they generally discouraged?
Nesting ternary operators to handle multiple conditions. Discouraged because they can quickly become unreadable and difficult to maintain. Use if-else if-else instead for complex logic.
32
What are some Common Pitfalls to avoid when using the Ternary Operator?
Readability Concerns: Avoid excessive nesting. Side Effects: Don't use ternary operators when valueIfTrue or valueIfFalse have side effects (e.g., modifying external variables). Multiple Statements: Use if-else for multiple statements within a conditional block. Complex Logic: Stick to if-else for complex or multi-step conditions.
33
When should you NOT use the Ternary Operator?
When the logic is complex. When side effects are involved. When multiple statements need to be executed. When readability suffers.
34
Convert the following if-else statement to a Ternary Operator: Generated javascript let score = 70; let result; if (score >= 60) { result = "Pass"; } else { result = "Fail"; }
Generated javascript let score = 70; let result = score >= 60 ? "Pass" : "Fail";
35
Explain why using boolean variables for conditions (instead of direct comparisons inside the ternary) can improve readability.
It makes the intent of the condition clearer and separates the condition evaluation from the value assignment. Generated javascript let hasPermission = user.role === "admin"; let message = hasPermission ? "Access Granted" : "Access Denied";
36
What is the Ternary Operator in JavaScript?
A concise way to write a conditional expression. Shorthand for a simple if-else statement. Syntax: condition ? valueIfTrue : valueIfFalse
37
Explain the Syntax of the Ternary Operator.
condition: An expression that evaluates to true or false. ?: The ternary operator symbol. valueIfTrue: The value returned if the condition is true. :: Separates the valueIfTrue and valueIfFalse. valueIfFalse: The value returned if the condition is false.
38
Give an example of a Ternary Operator in Action.
Generated javascript let age = 20; let isAdult = age >= 18 ? true : false; // isAdult will be true console.log(isAdult); // Output: true
39
What is Inline Conditional Assignment, and how does the Ternary Operator help with it?
Assigning a value to a variable based on a condition. Ternary operators provide a concise way to achieve this in a single line. Generated javascript let isLoggedIn = true; let userStatus = isLoggedIn ? "Online" : "Offline";
40
How is the Ternary Operator used in Conditional Rendering (e.g., in React)?
To conditionally render different elements or components based on a condition. Generated jsx {isLoggedIn ?

Welcome!

:

Please log in.

}
41
What are Chained Ternary Operations? Why are they generally discouraged?
Nesting ternary operators to handle multiple conditions. Discouraged because they can quickly become unreadable and difficult to maintain. Use if-else if-else instead for complex logic.
42
What are some Common Pitfalls to avoid when using the Ternary Operator?
Readability Concerns: Avoid excessive nesting. Side Effects: Don't use ternary operators when valueIfTrue or valueIfFalse have side effects (e.g., modifying external variables). Multiple Statements: Use if-else for multiple statements within a conditional block. Complex Logic: Stick to if-else for complex or multi-step conditions.
43
When should you NOT use the Ternary Operator?
When the logic is complex. When side effects are involved. When multiple statements need to be executed. When readability suffers.
44
Convert the following if-else statement to a Ternary Operator: Generated javascript let score = 70; let result; if (score >= 60) { result = "Pass"; } else { result = "Fail"; }
Generated javascript let score = 70; let result = score >= 60 ? "Pass" : "Fail";
45
Explain why using boolean variables for conditions (instead of direct comparisons inside the ternary) can improve readability.
It makes the intent of the condition clearer and separates the condition evaluation from the value assignment. Generated javascript let hasPermission = user.role === "admin"; let message = hasPermission ? "Access Granted" : "Access Denied";