Skip to main content

Comparisons Operators

JavaScript Fundamentals: Comparison Operators

Do all comparison operators return a Boolean value?

View Answer:
Interview Response: All JavaScript comparisons return a true or false Boolean value.

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”.

Code Example:

alert(2 > 1); // true (correct)
alert(2 == 1); // false (wrong)
alert(2 != 1); // true (correct)

How does JavaScript compare strings to see if their greater or less than another?

View Answer:
Interview Response: JavaScript uses “lexicographical” order. JavaScript compares letter-by-letter based on their Unicode value.

Code Example:

alert('Z' > 'A'); // true
alert('Glow' > 'Glee'); // true
alert('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.

Code Example:

alert('2' > 1); // true, string '2' becomes a number 2
alert('01' == 1); // true, the string '01' becomes a number 1

Code Example: For Boolean values, true becomes 1 and false becomes 0:

alert(true == 1); // true
alert(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;
alert(Boolean(a)); // false

let b = '0';
alert(Boolean(b)); // true

alert(a == b); // true!, equality operator

alert(a === b); // false!, strict equality operator

What is the difference between a regular and strict equality check?

View Answer:
Interview Response: The primary difference is strict equality does a value type check without any conversion.

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:

alert(0 === false); // false, because the types are different

Is there a benefit of using the strict equality operator?

View Answer:
Interview Response: The strict equality operator is a bit longer to write but makes it obvious what is going on and leaves less room for errors.

What value returns when null and undefined get compared using the strict equality operator?

View Answer:
Interview Response: 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
alert(null === undefined); // false

// Regular Equality Check
alert(null == undefined); // true

Is comparing undefined to other values a good or bad idea?

View Answer:
Interview Response: Bad Idea, You should never compare undefined to other values.

Code Example:

alert(undefined > 0); // false (1)
alert(undefined < 0); // false (2)
alert(undefined == 0); // false (3)

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.

  1. Except for strict equality ===, treat any comparison with undefined/null with extreme caution.
  2. 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.

How does the if statement work?

View Answer:
Interview Response: The “if” statement evaluates a condition and executes a code block if the result is true.

Code Example:

let year = prompt('In which year was ECMAScript-2015 published?', '');
if (year == 2015) alert('You are right!'); // You are right!