Prescreen Interview Quiz Questions Flashcards
Describe event bubbling
Event bubbling is a mechanism in the DOM where an event triggered on a child element propagates up through its ancestors. When an event is fired on an element, the event first runs the handlers on the element itself, then on its parent, and so on upwards through the DOM tree to the root.
This is useful for event delegation, as you can attach a single event listener to a parent element to catch events from all its descendants.
To stop event bubbling, you can use event.stopPropagation()
.
Explain Function.prototype.bind
The .bind
method creates a new function that, when called, has its this
keyword set to the provided value. The method also allows you to partially apply arguments to the function. Essentially, bind
enables you to execute a function with a specific this
context and with predefined initial arguments, if needed.
bind()
is most useful for binding the value of this
in methods of classes that you want to pass into other functions. This was frequently done in React class component methods which were not defined using arrow functions.
What is the CSS display
property and can you give a few examples of its use?
The display CSS property sets whether an element is treated as a block or inline box and can also control layout used for its children.
-
block
: Element takes full width, new line before and after. -
inline
: No new line, takes necessary width. -
inline-block
: Likeinline
, but can set width/height. -
flex
: Enables flexbox layout for children. -
grid
: Enables grid-based layout. -
none
: Element not displayed.
Examples:
- display: block;
for divs, headings.
- display: inline;
for spans, links.
- display: flex;
for creating flexible UI layouts.
Why is it generally a good idea to position CSS <link>
s between <head></head>
and JS <script>
s just before </body>
?
In a nutshell, such a placement of CSS <link>
s and JavaScript <script>
s allows for faster rendering of the page and better overall performance.
-
CSS in
<head>
: Placing CSS links in the<head>
ensures that styles are applied as soon as the HTML is loaded, improving the perceived performance and avoiding FOUC (Flash of Unstyled Content). -
JS before
</body>
: JavaScript files are usually placed just before the closing</body>
tag to prevent blocking the rendering of the page. JavaScript is blocking by nature; it stops HTML parsing until the script is downloaded and executed.-
Non-blocking alternatives: Modern practices include using attributes like
async
anddefer
to load JavaScript files without blocking HTML parsing, which gives you more flexibility on script positioning.
-
Non-blocking alternatives: Modern practices include using attributes like
What is the difference between ==
and ===
?
The ==
operator performs loose equality comparison, meaning it converts the operands to the same type before making the comparison. The ===
operator performs strict equality comparison, meaning it does not convert the operands to the same type.
-
==
: 5 == “5” returnstrue
-
===
: 5 === “5” returnsfalse
Use ===
for more predictable results and to avoid unintended type coercion.
Explain how a browser determines what elements match a CSS selector.
The browser uses a process known as the “Cascade” to determine which elements match a CSS selector. When a CSS rule is applied, the browser starts by parsing the selectors from right to left. It checks elements in the DOM against the far-right selector and moves through ancestors to see if they match the preceding selectors.
The matching process also takes into account the specificity of the selector. A more specific selector will override the styles of a less specific one.
What’s a typical use case for anonymous functions?
- Encapsulating code in IIFEs to prevent variable leakage to the global scope.
- Single-use callbacks, often making code more self-contained and readable.
- Arguments to functional programming methods like
map
,filter
, or in libraries like Lodash.
What are the differences between variables created using let
, var
or const
?
Variables declared using the var
keyword are scoped to the function in which they are created, or if created outside of any function, to the global object. let
and const
are block scoped, meaning they are only accessible within the nearest set of curly braces (function, if-else block, or for-loop).
var
allows variables to be hoisted, meaning they can be referenced in code before they are declared. let
and const
will not allow this, instead throwing an error.
Redeclaring a variable with var
will not throw an error, but let
and const
will.
let
and const
differ in that let allows reassigning the variable’s value while const
does not.
What’s the difference between .call
and .apply
in JavaScript?
Both .call
and .apply
are methods to invoke functions and set the this
context, but they differ in how they handle function arguments.
-
.call
: Takes a list of arguments after thethis
argument -func.call(thisArg, arg1, arg2, ...);
-
.apply
: Takes an array-like object for arguments after thethis
argument -func.apply(thisArg, [arg1, arg2, ...]);
What’s the difference between a relative
, fixed
, absolute
and static
-ally positioned element?
Each of these values for the position
property in CSS controls how an element is positioned within its parent or within the viewport.
-
Static: Default positioning. The element flows into its natural position in the document layout -
top
,right
,bottom
,left
properties have no effect. -
Relative: Positioned relative to its normal position -
top
,right
,bottom
,left
properties will move the element from its normal position. -
Fixed: Positioned relative to the browser viewport -
top
,right
,bottom
,left
properties will position the element in relation to the viewport. -
Absolute: Positioned relative to the nearest positioned ancestor, if any; otherwise relative to the initial containing block (often the
<html>
element) -top
,right
,bottom
,left
properties will position the element in relation to its containing block. -
Sticky: A mix of relative and fixed positioning. The element is treated as
relative
until it crosses a specified threshold, at which point it becomesfixed
-top
,right
,bottom
,left
properties will position the element within its container until a certain scroll position is reached.
Is there any reason you’d want to use translate()
instead of absolute
positioning, or vice-versa? And why?
Using translate()
and absolute
positioning serve different purposes and have distinct performance implications.
- Layout Impact:
-
translate()
: Element occupies its original space, likeposition: relative
. Doesn’t affect surrounding elements. -
absolute
: Removes the element from the flow of the page, affecting positioning of surrounding elements.
-
- Performance:
-
translate()
: More efficient, triggers only compositions and uses the GPU. Ideal for smoother, more efficient animations. -
absolute
: Triggers reflows and repaints, using the CPU. Can impact performance negatively.
-
Explain “hoisting”
Hoisting is a JavaScript behavior where variable and function declarations are moved, or “hoisted,” to the top of their containing scope during compilation. However, only the declarations are hoisted, not the initializations. With var
, the variable is hoisted and initialized with undefined
. With let
and const
, the variable is hoisted but remains uninitialized (in the “Temporal Dead Zone”). Function declarations are also hoisted, but function expressions are not.
For example, a variable declared with var
is accessible before its declaration, but it will be undefined
.
Describe the difference between a cookie, sessionStorage
and localStorage
.
All three are client-side storage technologies but differ in lifespan, capacity, and accessibility.
Cookie
- Lifespan: Can be set manually, otherwise session-based.
- Capacity: Up to 4KB.
- Accessibility: Both server and client-side. Sent with each HTTP request, affecting performance.
- Scope: Domain-specific.
sessionStorage
- Lifespan: Limited to a session (tab/window). Cleared when the session ends.
- Capacity: 5-10MB depending on the browser.
- Accessibility: Client-side only.
- Scope: Tab-specific.
localStorage
- Lifespan: Persistent, until explicitly deleted.
- Capacity: 5-10MB depending on the browser.
- Accessibility: Client-side only.
- Scope: Shared across all tabs/windows from the same domain.
Cookies are suitable for small bits of data and for server-client communication. sessionStorage and localStorage are useful for larger amounts of client-side-only data storage, with sessionStorage being ephemeral and localStorage being persistent.
Explain your understanding of the box model in CSS
The CSS box model describes the layout of each element on a web page. It consists of:
- Content: The box content, defined by width and height.
- Padding: Space between the content.
- Border: Goes around the padding and content.
- Margin: Space between the border and the outside edge of the element.
Different Box Models:
-
Content-Box: This is the default value of the
box-sizing
property. The width and height properties are applied only to the element’s content, not its padding or border -box-sizing: content-box;
-
Border-Box: The width and height properties include content, padding, and border, but not the margin -
box-sizing: border-box;
You can globally set all elements to use the border-box
model, via a CSS rule.
Explain how this
works in JavaScript.
The overarching rule is that this
is determined at the time a function is invoked by inspecting where it’s called, its call site. It follows these rules, in order of precedence:
-
New Keyword: If the function is called with
new
,this
inside the function will be a brand new object. Without it will refer to global object (see 4 below) -
Apply, Call, Bind: When using
apply
,call
, orbind
to call/create a function,this
will be the object passed as an argument. -
Method Invocation: When a function is called as a method, i.e.,
obj.method()
,this
will be the object that owns the method. -
Free Function Invocation / Global: If none of the conditions above are met,
this
defaults to the global object. In a browser, it’s thewindow
object; in strict mode ('use strict'
), it’sundefined
. - Rule Precedence: If multiple rules apply, the one higher on this list takes precedence.
-
Arrow Functions: ES2015 arrow functions ignore all these rules and take on the
this
value of their surrounding scope value of its surrounding scope at the time it’s created.