let val = 10; if (typeof val = "number") { console.log("It's a number"); }
medium
A. Using single equals (=) instead of comparison operator
B. Missing semicolon after let declaration
C. Incorrect quotes around "number"
D. typeof cannot be used on variables
Solution
Step 1: Check the if condition syntax
The condition uses = which is assignment, not comparison.
Step 2: Correct operator for comparison
Use === or == to compare the result of typeof val with "number".
Final Answer:
Using single equals (=) instead of comparison operator -> Option A
Quick Check:
Use === for comparison, not = [OK]
Hint: Use === for comparison, not = [OK]
Common Mistakes:
Using = instead of === in if conditions
Thinking typeof returns variable type directly
Ignoring quotes around type string
5. You want to write a function isStringOrNumber that returns true if the input is either a string or a number, and false otherwise. Which of the following implementations is correct?
hard
A. function isStringOrNumber(value) { return typeof value === 'string' || typeof value === 'number'; }
B. function isStringOrNumber(value) { return typeof value === 'string' && typeof value === 'number'; }
C. function isStringOrNumber(value) { return typeof value == 'string' && typeof value == 'number'; }
D. function isStringOrNumber(value) { return typeof value = 'string' || typeof value = 'number'; }
Solution
Step 1: Understand the requirement
The function should return true if the value is a string OR a number.
Step 2: Check logical operators and comparisons
function isStringOrNumber(value) { return typeof value === 'string' || typeof value === 'number'; } uses || (OR) and strict equality ===, which is correct.
Step 3: Identify errors in other options
function isStringOrNumber(value) { return typeof value === 'string' && typeof value === 'number'; } and the option using == uses AND &&, which requires both types simultaneously (impossible). function isStringOrNumber(value) { return typeof value = 'string' || typeof value = 'number'; } uses assignment = instead of comparison.
Final Answer:
function isStringOrNumber(value) { return typeof value === 'string' || typeof value === 'number'; } -> Option A
Quick Check:
Use OR (||) and === for type checks [OK]
Hint: Use || for OR and === for type checks [OK]
Common Mistakes:
Using && instead of || for OR condition
Using = instead of === for comparison
Mixing == and === inconsistently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.