What is the difference between == and === in JavaScript?
== (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.
Give an example where == returns true but === returns false.
console.log(1 == “1”); // true (Type coercion: string “1” to number 1)
console.log(1 === “1”); // false (Different types: number and string)
What are != and !==? How do they differ?
!= (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.
What is “type coercion”? Why is it important to understand?
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 !=.
Explain how strings are compared using <, >, <=, and >=.
Strings are compared lexicographically (dictionary order) based on the Unicode values of their characters. The comparison proceeds character by character from left to right.
Is “apple” < “Banana” true or false? Why?
false. Uppercase letters have lower Unicode values than lowercase letters. Therefore, “B” < “a”.
How would you perform a case-insensitive string comparison in JavaScript?
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)
What happens when you compare a string and a number using <, >, <=, or >=?
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.
Why should you generally avoid using == in JavaScript?
Because its type coercion can lead to unexpected and difficult-to-debug behavior. === provides more predictable and reliable comparisons.
What is the result of null == undefined and null === undefined?
null == undefined is true (a special case in loose equality).
null === undefined is false (they are different types).
Explain the pitfall of comparing objects (like {} or []) using == or ===.
== 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.
What will the following code output?
console.log(“2” > “12”);
true. String comparison is lexicographical. “2” comes after “1” in dictionary order, so ‘2’ > ‘12’ is true.
How do you numerically compare strings that contain numbers?
Convert them to numbers first using Number(), parseInt(), or parseFloat().
console.log(Number(“10”) < Number(“2”)); // false
What are the three JavaScript logical operators?
&& (AND), || (OR), ! (NOT)
Explain the purpose of the && (AND) operator. What must be true for A && B to evaluate to true?
The && operator returns true if both operands are true. It returns false otherwise. Both A and B must be true.
Explain the purpose of the || (OR) operator. What must be true for A || B to evaluate to true?
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.
Explain the purpose of the ! (NOT) operator. What does it do to a boolean value?
The ! operator inverts the boolean value of its operand. !true is false, and !false is true. It negates the truthiness/falsiness of the value.
What is “short-circuit evaluation” and how does it apply to the && (AND) operator?
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.
What is “short-circuit evaluation” and how does it apply to the || (OR) operator?
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.
What is the order of precedence for logical operators?
! (NOT) has the highest precedence, followed by && (AND), and then || (OR). Use parentheses () to override the default order.
What are some common use cases for logical operators?
Validating Input
Controlling Program Flow
Access Control
Error Handling
Creating complex conditional expressions
What are “truthy” and “falsy” values? Give some examples of falsy values.
“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.
Why is readability important when working with logical operators? How can you improve the readability of your code when using logical operators?
Use parentheses to make evaluation order explicit.
Use descriptive variable names.
Break down complex conditions.
Add comments.
Avoid overly complex expressions.
Give an example of using logical operators to validate if a number is within a specific range.
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.”);
}
Welcome!
:Please log in.
}Welcome!
:Please log in.
}