Question Bank
JavaScript Coercion and Equality Code Traces
Difficulty: Easy
Six output traces drilling on `==` vs `===`, unary `+`, falsy values, and chained relational coercion. Sharpens the rules JS applies before comparing.
JavaScript Coercion and Equality Code Traces
Six output traces drilling on `==` vs `===`, unary `+`, falsy values, and chained relational coercion. Sharpens the rules JS applies before comparing.
Question Bank
Easy
JavaScript
6 questions
quiz
interview-prep
fundamentals
js-language
440 views
10
What does the following code print?
Examples
Example 1:
Input: console.log(a == b); console.log(a === b);
Output: false; false
Explanation: Arrays are objects, so both == and === compare by reference identity, and a and b are two distinct array instances.What is the output of the following code?
Examples
Example 1:
Input: console.log(0 == '0'); console.log(0 === '0');
Output: true; false
Explanation: == coerces the string '0' to the number 0 before comparing, while === requires identical types and a number is not a string.What does the following code print, line by line?
Examples
Example 1:
Input: log(+'0'); log(+'false'); log(+1); log(+'1'); log(+''); log(+true); log(+false); log(+'foo');
Output: 0; NaN; 1; 1; 0; 1; 0; NaN
Explanation: Unary + applies Number(x) semantics, so numeric strings and booleans coerce cleanly while non-numeric strings become NaN.Trace the output of each console.log below.
Examples
Example 1:
Input: undefined + 1; undefined == null; undefined === null; null * 2; null * null; 1 === true
Output: NaN; true; false; 0; 0; false
Explanation: undefined coerces to NaN in arithmetic and null coerces to 0, while undefined == null is a special spec rule and === never coerces.What does each line below print?
Examples
Example 1:
Input: ![] == 0; [] == ![]; '1' == 1; '01' == true
Output: true; true; true; true
Explanation: Each comparison reduces to 0 == 0 or 1 == 1 after == coerces booleans, arrays, and strings into numbers.Predict the output and explain why chained relational operators behave this way.
Examples
Example 1:
Input: 5 < 6 < 7; 7 > 6 > 5
Output: true; false
Explanation: Relational operators are left-associative, so the first inner comparison yields a boolean that coerces to 1 or 0 before the next comparison runs.