Sample questions
Javascript LanguageDifficulty 1
In JavaScript, what is the main difference between let and const?
- aA
const binding cannot be reassigned after its initial assignment✓ - bA
const variable is only accessible inside the function that declares it - cA
let variable is not hoisted, while a const variable is - dA
const also prevents the contents of an object or array from changing
Explanation:const forbids reassigning the binding itself, while let allows reassignment; both are block-scoped, so (b) is wrong. const does not freeze the value it points to — the properties of a const object can still be mutated, which makes (d) a very common misconception.
Javascript LanguageDifficulty 2
console.log(a);
console.log(b);
var a = 1;
let b = 2;
What happens when this code runs?
- aIt prints
undefined and then undefined - bIt prints
1 and then 2 - cIt prints
undefined, then throws a ReferenceError for b✓ - dIt throws a ReferenceError on the very first line
Explanation:var a is hoisted and initialized to undefined, so reading a before its assignment prints undefined. let b is hoisted too but stays in the temporal dead zone until its declaration, so reading it early throws a ReferenceError. Because a is logged first and succeeds, the error is not on the first line (d).
Javascript LanguageDifficulty 2
function makeCounter() {
let count = 0;
return function () {
count += 1;
return count;
};
}
const next = makeCounter();
console.log(next(), next(), next());
What does this print?
- a
1 1 1, because count resets to 0 on every call - b
1 2 3, because the inner function still sees the same count✓ - c
0 1 2, because count is returned before it is incremented - dIt throws, because
count is not defined inside the inner function
Explanation:makeCounter returns an inner function that closes over count; that variable lives on between calls instead of resetting, so the three calls yield 1, 2, 3. The increment happens before return, so it never prints 0 first (c), and closure is exactly why count is still visible inside (d).
Javascript LanguageDifficulty 2
In a regular (non-arrow) function called as obj.method(), what does this refer to?
- aThe global object, regardless of how the function is called
- bThe function object itself
- cWhatever value was passed as the first argument to the function
- dThe object the method was called on — here,
obj✓
Explanation:For a regular function, this is set by the call site; calling it as a method binds this to the object left of the dot. When the same function is called plain (not as a method), this falls back to the global object or undefined in strict mode — which is why (a) is only true for that unbound case.
Javascript LanguageDifficulty 2
console.log(0 == '');
console.log(0 == '0');
console.log('' == '0');
What does this print, in order?
- a
true, true, true - b
true, true, false✓ - c
false, false, false - d
true, false, false
Explanation:With ==, both sides are coerced to numbers when comparing a number to a string: 0 == '' becomes 0 == 0 (true) and 0 == '0' becomes 0 == 0 (true). But '' == '0' compares two strings directly, with no coercion, so it is false. Using === would make all three false because the types differ.
Javascript LanguageDifficulty 2
Why do many JavaScript style guides recommend using === instead of ==?
- a
=== is faster because it does not have to read the right-hand operand at all - b
== is deprecated and will be removed from the language - c
=== compares without type coercion, so results are more predictable✓ - d
=== can compare objects by their contents, while == cannot
Explanation:== applies type coercion rules that are easy to get wrong (for example 0 == '' is true), while === requires both the type and the value to match, avoiding surprises. == is not deprecated (b), and neither operator compares objects by content — both compare object references (d).