JavaScript Middle interview questions and answers
75 questions for a JavaScript (Middle) interview - with correct answers, explanations and links to the docs.
Take this quiz- 1.What does this code print?
- 2.How do
call,applyandbinddiffer from each other? - 3.What does this code print?
- 4.What does this code print after a second?
- 5.What does this code print?
- 6.How does the engine look up a property the object itself doesn't have?
- 7.What does
Object.create(proto)do? - 8.What are classes in JavaScript really?
- 9.Why must a subclass constructor call
super()before accessingthis? - 10.How does
#countdiffer from_countin a class? - 11.How does
obj instanceof Classwork? - 12.What does this code print?
- 13.What was an IIFE
(function () { ... })()used for in the past? - 14.What is currying?
- 15.How does debounce differ from throttle?
- 16.What is memoization?
- 17.How does
Promise.allbehave if one of the promises rejects? - 18.What does
Promise.allSettledreturn? - 19.How does
Promise.anydiffer fromPromise.race? - 20.How long will this code take if each request lasts 1 second?
- 21.Why doesn't
forEachwait for async callbacks? - 22.What does the chain print?
- 23.What does a promise's
.finally()do? - 24.In what order are the lines printed?
- 25.Why is a recursive
setTimeoutsometimes preferable tosetInterval? - 26.What is
requestAnimationFramefor? - 27.What does a generator function do?
- 28.What does your own object need to work with
for...ofand spread? - 29.What is
Symbolused for? - 30.How does
WeakMapdiffer fromMap? - 31.Why is
structuredClone(obj)better thanJSON.parse(JSON.stringify(obj))? - 32.What does this code print?
- 33.What does
Object.defineProperty(obj, 'id', { value: 1, writable: false })give you? - 34.What does
[10, 1, 2].sort()return? - 35.What do
parseInt('12px')andNumber('12px')return? - 36.How do ES modules differ from CommonJS?
- 37.Why do you need dynamic
import()? - 38.Will
fetchreject the promise on a 404 response? - 39.How do you cancel a
fetchrequest? - 40.What is CORS?
- 41.Why is a cookie with the
HttpOnlyflag safer than a regular one? - 42.How does
stopPropagationdiffer fromstopImmediatePropagation? - 43.What does the
{ passive: true }option inaddEventListener('touchmove', ...)do? - 44.How do you efficiently insert 1000 elements into the DOM?
- 45.What's the most convenient way to detect that an element entered the viewport?
- 46.What's the right way to create a custom error class?
- 47.What does
Object.fromEntries(Object.entries({ a: 1, b: 2 }).map(([k, v]) => [k, v * 10]))return? - 48.How does a
getterdiffer from a regular property? - 49.What does
Array.from({ length: 3 }, (_, i) => i * 2)return? - 50.What does
{} === {}return? - 51.In what order are the lines printed?
- 52.Why does this
catchnot handle the rejection? - 53.How does
arr.toSorted()differ fromarr.sort()? - 54.What does
"a-b-c".replace("-", "+")return? - 55.Why are event handlers often written as arrow-function class fields?
- 56.What does this code print?
- 57.What does
new Array(3).map((_, i) => i)return? - 58.Which error does
undefinedVariable.foothrow, and whichnull.foo? - 59.What does
typeof notDeclaredreturn ifnotDeclaredwas never declared? - 60.What happens when this code runs?
- 61.What does
new User("Ann")do with a regular constructor function? - 62.How does
Object.assign(target, source)differ from{ ...target, ...source }? - 63.Why do you need two
awaits here? - 64.What is the easiest way to send all fields of a
<form>withfetch? - 65.How do you notify other parts of the page about something with a DOM event?
- 66.Which property should you use to detect the pressed key?
- 67.What does
options.onSuccess?.(data)do? - 68.What does
config.timeout ??= 5000do? - 69.Why convert a large list of allowed IDs to a
Setbefore many lookups? - 70.How can a function keep private state without classes?
- 71.What does
event.target.closest("[data-id]")return? - 72.Why does
el.style.widthreturn an empty string for a width set in CSS? - 73.How should money be stored to avoid
0.1 + 0.2problems? - 74.What does this code print?
- 75.What does
arr.with(1, "x")return forarr = ["a", "b", "c"]?