What does the following JavaScript code print (console output, as in Node.js)?
console.log("10" - 4, "10" * "2", "10" / "4", "abc" * 2);
-, * and / convert strings to numbers. "abc" converts to NaN, and any arithmetic with NaN is NaN.
Operators · Exercise 2 of 4
Read each JavaScript program and choose what it prints. Every program was run on Node.js as a normal (non-strict) script unless it says "use strict", so the answers are the real output.
What does the following JavaScript code print (console output, as in Node.js)?
console.log("10" - 4, "10" * "2", "10" / "4", "abc" * 2);
-, * and / convert strings to numbers. "abc" converts to NaN, and any arithmetic with NaN is NaN.
What does the following JavaScript code print (console output, as in Node.js)?
let i = 1;
const r = i++ + ++i;
console.log(i, r);
Left to right: i++ gives 1 and makes i 2; ++i makes i 3 and gives 3. r = 1 + 3 = 4.
What does the following JavaScript code print (console output, as in Node.js)?
console.log("1" == 1, true == 1, "true" == true, [] == false);
== converts booleans to numbers: "true" == true becomes "true" == 1, then NaN == 1 (false). [] == false becomes [] == 0, then "" == 0, then 0 == 0 (true).
What does the following JavaScript code print (console output, as in Node.js)?
console.log(null == 0, null >= 0, undefined == 0, undefined >= 0);
== never converts null/undefined to numbers. Relational operators convert null to 0 (0 >= 0 is true) and undefined to NaN (every comparison with NaN is false).
What does the following JavaScript code print (console output, as in Node.js)?
console.log(0 || "" || null || "found", 1 && "a" && 0 && "b");
|| returns the first truthy operand ("found"); && returns the first falsy operand (0) and stops there, so "b" is never reached.
What does the following JavaScript code print (console output, as in Node.js)?
const count = 0;
console.log(count || 10, count ?? 10);
0 is falsy, so || returns 10. 0 is not null or undefined, so ?? keeps it.
What does the following JavaScript code print (console output, as in Node.js)?
const user = { profile: null };
console.log(user.profile?.email, user.address?.city ?? "unknown");
user.profile is null, so ?.email gives undefined. user.address is undefined, so ?.city gives undefined and ?? substitutes "unknown".
What does the following JavaScript code print (console output, as in Node.js)?
console.log("2" > "12", 2 > "12", "b" > "a");
Two strings compare character by character: "2" > "1", so "2" > "12". With one number, the string is converted: 2 > 12 is false.
What does the following JavaScript code print (console output, as in Node.js)?
console.log(3 > 2 > 1, 1 < 2 < 3);
3 > 2 is true, and true > 1 becomes 1 > 1 (false). 1 < 2 is true, and true < 3 becomes 1 < 3 (true).
What does the following JavaScript code print (console output, as in Node.js)?
console.log(JSON.stringify([] + []), [] + {}, [1, 2] + [3]);
+ converts objects to primitives: [] becomes "", {} becomes "[object Object]", [1, 2] becomes "1,2". Then the strings are concatenated.
Timed online tests with instant scores and explanations.