Frontend Flashcards

(31 cards)

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the DOM in the context of web development?

A

The DOM (Document Object Model) is a tree-like representation of a webpage’s structure, allowing JavaScript to manipulate HTML elements dynamically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between inline, block, and inline-block in CSS?

A

inline elements (e.g., <span>) flow within text; block elements (e.g., <div>) take full width; inline-block allows inline flow with block properties like width/height.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does CSS specificity work?

A

Specificity determines which CSS rule applies, based on a score: inline styles (1000), IDs (100), classes/attributes (10), elements (1). Higher score wins.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a CSS Flexbox, and how is it used?

A

Flexbox is a layout model for aligning items in a container. Example: display: flex; justify-content: space-between; aligns children horizontally with spacing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain CSS Grid and its advantages.

A

CSS Grid creates two-dimensional layouts. Example: display: grid; grid-template-columns: 1fr 1fr; makes two equal columns. Advantages: precise control, responsive design.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you center a div horizontally and vertically using CSS?

A

```css
div {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
/* Explanation: Flexbox centers content in both axes (O(1) layout). */
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between relative, absolute, and fixed positioning in CSS?

A

relative moves an element relative to its normal position; absolute relative to nearest positioned ancestor; fixed relative to viewport, stays on scroll.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is event bubbling in JavaScript?

A

Event bubbling is when an event on a child element propagates to its ancestors. Example: A click on a <button> inside a <div> triggers the <div>’s click handler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you prevent default browser behavior in JavaScript?

A

```javascript
element.addEventListener(‘click’, (e) => {
e.preventDefault();
// Custom logic
});
// Explanation: e.preventDefault() stops default actions, like form submission.
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the purpose of addEventListener in JavaScript?

A

addEventListener attaches an event handler to an element without overwriting existing ones. Example: button.addEventListener('click', () => alert('Clicked'));.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a closure in JavaScript, and how is it used in frontend?

A

A closure is a function retaining access to its outer scope’s variables. Example: function counter() { let count = 0; return () => count++; } used for state encapsulation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does this behave in a DOM event handler?

A

In a DOM event handler, this refers to the element triggering the event. Example: <button onclick='console.log(this)'> logs the button element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a React component, and how do you create one?

A

A React component is a reusable UI piece returning JSX. Example: function MyComponent() { return <div>Hello</div>; } or class MyComponent extends React.Component { render() { return <div>Hello</div>; } }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of useState in React?

A

useState manages state in functional components, returning a state value and updater. Example: const [count, setCount] = useState(0);.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does the useEffect hook work in React?

A

useEffect handles side effects after render, like API calls. Example: useEffect(() => { fetchData(); }, [id]); runs when id changes.

17
Q

What is a controlled component in React?

A

A controlled component ties form input values to state. Example: <input value={state} onChange={(e) => setState(e.target.value)} />.

18
Q

How do you optimize React performance with React.memo?

A

React.memo memoizes a component to prevent re-renders unless props change. Example: const Memoized = React.memo(MyComponent);.

19
Q

What is the virtual DOM in React?

A

The virtual DOM is an in-memory representation of the real DOM. React diffs it to minimize actual DOM updates, improving performance.

20
Q

How do you handle forms in React with multiple inputs?

A

```javascript
function Form() {
const [form, setForm] = useState({ name: ‘’, email: ‘’ });
const handleChange = (e) => setForm({ …form, [e.target.name]: e.target.value });
return (
<form>
<input name=’name’ value={form.name} onChange={handleChange} />
<input name=’email’ value={form.email} onChange={handleChange} />
</form>
);
}
// Explanation: Uses a single state object for multiple inputs (O(1) updates).
~~~

21
Q

What is the purpose of useContext in React?

A

useContext accesses context to share data without prop drilling. Example: const value = useContext(MyContext); retrieves context value.

22
Q

How do you implement lazy loading in React?

A

```javascript
const LazyComponent = React.lazy(() => import(‘./Component’));
function App() {
return (
<Suspense fallback={<div>Loading…</div>}>
<LazyComponent></LazyComponent>
</Suspense>
);
}
// Explanation: React.lazy and Suspense defer component loading (O(1) setup).
~~~

23
Q

What is a CSS preprocessor, and name an example?

A

A CSS preprocessor adds features like variables and nesting. Example: Sass ($color: red; .box { background: $color; }) compiles to CSS.

24
Q

How do you handle responsive design in CSS?

A

Use relative units (%, vw, rem), media queries, and frameworks. Example: @media (max-width: 600px) { .box { font-size: 14px; } } adjusts for small screens.

25
What is the purpose of the `z-index` property in CSS?
`z-index` controls the stacking order of positioned elements. Higher values appear in front. Example: `z-index: 10;` places an element above `z-index: 5;`.
26
How do you debounce a function in JavaScript for performance?
```javascript function debounce(func, wait) { let timeout; return (...args) => { clearTimeout(timeout); timeout = setTimeout(() => func(...args), wait); }; } // Example: debounce(() => search(), 300) limits rapid calls (O(1)). ```
27
What is the difference between `localStorage` and `sessionStorage`?
`localStorage` persists until cleared; `sessionStorage` lasts until the tab closes. Both store key-value pairs. Example: `localStorage.setItem('key', 'value');`.
28
How do you improve frontend performance with code splitting?
Code splitting loads only necessary JavaScript. In React: Use `React.lazy` or Webpack’s dynamic imports to split bundles, reducing initial load time.
29
What is a service worker, and how is it used?
A service worker is a script for caching and offline functionality. Example: Register with `navigator.serviceWorker.register('/sw.js');` to cache assets.
30
How do you handle cross-browser compatibility?
Use feature detection, polyfills, and tools like Autoprefixer. Example: Add vendor prefixes (`-webkit-`) for CSS properties like `transform`.
31
What is the purpose of ARIA attributes in HTML?
ARIA (Accessible Rich Internet Applications) attributes enhance accessibility. Example: `role='button' aria-label='Close'` describes a custom button for screen readers.