Comparison Operators - JavaScript Interview
JavaScript Fundamentals: Comparison Operators
What is a comparison operator in JavaScript?
View Answer:
Interview Response: A comparison operator in JavaScript is used to compare two values and returns a boolean result (true or false) based on the comparison.
Code Example: Example showcasing the usage of comparison operators in JavaScript.
let num1 = 5;
let num2 = 10;
console.log(num1 > num2); // Output: false (Greater than)
console.log(num1 < num2); // Output: true (Less than)
console.log(num1 >= num2); // Output: false (Greater than or equal to)
console.log(num1 <= num2); // Output: true (Less than or equal to)
console.log(num1 == num2); // Output: false (Equality)
console.log(num1 === num2); // Output: false (Strict equality)
console.log(num1 != num2); // Output: true (Inequality)
console.log(num1 !== num2); // Output: true (Strict inequality)
In this example, the comparison operators are used to compare the values of num1
and num2
. The result of each comparison is a boolean value (true
or false
) based on the outcome of the comparison.
Do all comparison operators return a Boolean value?
View Answer:
Interview Response: All comparison operators in JavaScript return a Boolean value of either true or false.
Technical Response: Yes, every comparison yields a true or false Boolean value.
- true – means “yes”, “correct” or “the truth”.
- false – means “no”, “wrong” or “not the truth”.
- true – means “yes”, “correct” or “the truth”.
- false – means “no”, “wrong” or “not the truth”.
Code Example:
console.log(2 > 1); // true (correct)
console.log(2 == 1); // false (wrong)
console.log(2 != 1); // true (correct)
What are the different types of comparison operators in JavaScript?
View Answer:
Interview Response: JavaScript comparison operators include equality (==, ===), inequality (!=, !==), and relational (>, <, >=, <=) operators. They compare values for equality, inequality, or relative size, respectively.
What is the difference between inequality and strict inequality operators?
View Answer:
Interview Response: Inequality (!=) checks value inequality with type coercion, while strict inequality (!==) compares both value and type without coercion for accurate results.
Code Example:
console.log(5 != "5"); // Output: false (Inequality with type coercion)
console.log(5 !== "5"); // Output: true (Strict inequality without type coercion)
console.log(true != 1); // Output: false (Inequality with type coercion)
console.log(true !== 1); // Output: true (Strict inequality without type coercion)
Can you explain type coercion in the context of comparison operators?
View Answer:
Interview Response: Type coercion converts values to a common data type during comparisons. It occurs with loose comparison operators (==, !=) but not strict operators (===, !==).
How does JavaScript compare strings to see if their greater or less than another?
View Answer:
Interview Response: In JavaScript, strings are compared lexicographically, which means that the characters in the strings are compared one by one in order until a difference is found. The comparison is based on the Unicode values of the characters.
Code Example:
console.log('Z' > 'A'); // true
console.log('Glow' > 'Glee'); // true
console.log('Bee' > 'Be'); // true
// Unicode Values
let myLetter = 'Hello';
console.log(myLetter.charCodeAt(0)); // returns Unicode value 72
console.log(myLetter.charCodeAt(1)); // returns Unicode value 101
When comparing values of different types, does JavaScript convert the values to numbers?
View Answer:
Interview Response: Yes, when comparing values of different types, it converts the values to numbers. For Boolean values, true becomes 1 and false becomes 0.
Code Example:
console.log('2' > 1); // true, string '2' becomes a number 2
console.log('01' == 1); // true, the string '01' becomes a number 1
Code Example: For Boolean values, true becomes 1 and false becomes 0:
console.log(true == 1); // true
console.log(false == 0); // true
Is it possible that two values are equal at the same time if one is true as a Boolean and the other one is false as a Boolean?
View Answer:
Interview Response: When there is an explicit conversion to a Boolean on values. A string and a number, such as a number 0 and string “0”. The return value for the string returns true, and it returns false for the number. When we attempt to compare the two using the equality operator, the return value returns true, but it returns false with the strict equality operator.
Code Example:
let a = 0;
console.log(Boolean(a)); // false
let b = '0';
console.log(Boolean(b)); // true
console.log(a == b); // true!, equality operator
console.log(a === b); // false!, strict equality operator
What is the difference between a regular and strict equality check?
View Answer:
Interview Response: Equality (==) checks value equality with type coercion, while strict equality (===) checks both value and type without coercion, ensuring precise comparisons.
Technical Response: The regular equality check loosely compares values with type conversion. In contrast, the strict-equality check compares the value and the data type without converting the types.
Code Example:
console.log(0 === false); // false, because the types are different
What is the benefit of using the strict equality operator?
View Answer:
Interview Response: Yes, using the strict equality operator in JavaScript helps to avoid unexpected type coercion issues and ensures precise comparisons.
Interview Response: The benefit of using the strict equality (`===`) operator is that it ensures both the value and type are compared, providing more precise and predictable results, avoiding potential type coercion issues, and promoting code correctness.
Code Example:
console.log(5 === 5); // Output: true (Strict equality with matching value and type)
console.log(5 === "5"); // Output: false (Strict equality with different types)
console.log(true === 1); // Output: false (Strict equality with different types)
console.log(null === undefined); // Output: false (Strict equality with different types)
Using strict equality helps ensure that comparisons are performed accurately based on both value and type, reducing potential bugs and unexpected behavior.
What results from comparing "null" and "undefined" using equality and strict equality operators?
View Answer:
Interview Response: Using the equality operator (==), null and undefined are considered equal. However, with the strict equality operator (===), they are not equal, as they are different types.
What value returns when null and undefined get compared using the strict equality operator?
View Answer:
Interview Response: In JavaScript, when null and undefined are compared using the strict equality operator, they return false because they are not the same type.
Technical Response: False because each of them is a different type, but the non-strict operator returns true. For math and other comparisons such as greater and less than null/undefined are converted to numbers.
Code Example:
// Strict Equality Check
console.log(null === undefined); // false
// Regular Equality Check
console.log(null == undefined); // true
Is comparing undefined to other values a good or bad idea?
View Answer:
Interview Response: Comparing undefined to other values in JavaScript can be a bad idea as it can lead to unexpected results and errors.
Code Example:
console.log(undefined > 0); // false (1)
console.log(undefined < 0); // false (2)
console.log(undefined == 0); // false (3)
What is the behavior of comparison operators when comparing values with different types?
View Answer:
Interview Response: Loose comparison operators (==, !=) perform type coercion before comparing values, while strict operators (===, !==) compare values and types directly, avoiding coercion.
What are two ways to avoid problems with undefined/null values?
View Answer:
Interview Response: We should avoid using undefined or null in anything less than strict comparisons and never use comparisons with an undefined or null value.
Technical Response: There are two approaches to dealing with undefined and null values.
- Except for strict equality ===, treat any comparison with undefined/null with extreme caution.
- If you're not sure what you're doing, don't use comparisons >= and >= with a variable that might be null/undefined. Check for these values separately if a variable may have them.
What does it mean for two values to be "equal" in JavaScript?
View Answer:
Interview Response: Two values are "equal" in JavaScript if they have the same value after type coercion.
How do JavaScript comparison operators handle objects?
View Answer:
Interview Response: JavaScript comparison operators compare object references, not the contents. They check if the compared objects refer to the same memory location, rather than comparing their properties or values.
Code Example:
let obj1 = { name: "Alice" };
let obj2 = { name: "Alice" };
let obj3 = obj1;
console.log(obj1 === obj2); // Output: false (Different object references)
console.log(obj1 === obj3); // Output: true (Same object reference)
What does it mean for two values to be "identical" in JavaScript?
View Answer:
Interview Response: Two values are "identical" in JavaScript if they have the same value and the same type.
Can you compare the values of different types in JavaScript?
View Answer:
Interview Response: Yes, but the result may be unexpected due to type coercion in JavaScript.
Code Example:
console.log(5 == "5"); // Output: true (Equality with type coercion)
console.log(5 === "5"); // Output: false (Strict equality without type coercion)
console.log(true == 1); // Output: true (Equality with type coercion)
console.log(true === 1); // Output: false (Strict equality without type coercion)
console.log(null == undefined); // Output: true (Equality with type coercion)
console.log(null === undefined); // Output: false (Strict equality without type coercion)