You are interviewing for a frontend JavaScript position. Answer the following conceptual and practical questions.
1. JavaScript Event Loop, Microtasks, and Macrotasks
Explain how the JavaScript event loop works in a browser environment.
-
What is the
call stack
?
-
What is the
event loop
?
-
What are
macrotasks
(also called tasks)? Give common examples (e.g.,
setTimeout
,
setInterval
, DOM events, etc.).
-
What are
microtasks
? Give common examples (e.g.,
Promise.then
,
MutationObserver
, etc.).
-
In what order are microtasks and macrotasks executed relative to each other and to rendering/painting? Describe a typical sequence for one event loop tick.
2. Difference Between var and let
Explain the differences between var and let in JavaScript, focusing on:
-
Scope (function scope vs block scope).
-
Hoisting behavior.
-
The "temporal dead zone".
-
Re-declaration rules.
-
How they behave when declared in the global scope (relationship to the global object).
Optionally, briefly contrast const with let as well.
3. this Binding in JavaScript
Explain how the value of this is determined in the following contexts:
-
In the global scope.
-
Inside a regular function call (not attached to an object).
-
Inside a method call on an object.
-
Inside an event handler in the browser.
-
Inside an arrow function.
Then describe at least three different ways to explicitly control or fix the value of this (for example, with bind, call, apply, storing this in another variable, using arrow functions, etc.), and give short code examples.
4. Draw a 2x2 Square Grid with HTML and CSS
Using only HTML and CSS (no JavaScript), write markup and styles to draw a square that is divided into four equal smaller squares (a 2x2 grid), each with visible borders.
-
Assume the outer square should be, for example,
200px by 200px
.
-
The 4 inner squares should be equal in size and fill the outer square exactly.
-
Use any reasonable CSS layout technique (e.g., flexbox, CSS grid, or percentage-based widths/heights).
Provide the HTML and CSS code required.