JavaScript, DOM form the cornerstone of modern front-end development interviews. This combination tests candidates' understanding of client-side logic, component-based architecture, and direct manipulation of the browser environment. These topics are commonly evaluated at intermediate levels companies ranging from startups giants like Amazon, Google, and Facebook as they reflect real-world application development skills.
JavaScript is a high-level, dynamic programming language primarily used for web development to create interactive, client-side applications.
// Closure example function outer() { let count = 0; return function inner() { count++; return count; }; } const increment = outer(); console.log(increment()); // 1 console.log(increment()); // 2
React is a JavaScript library for building user interfaces, emphasizing component-based architecture and declarative UI.
useState, useEffect, etc.) that enable state and side effects in functional components.import React, { useState, useEffect } from 'react'; function Counter() { const [count, setCount] = useState(0); useEffect(() => { document.title = `You clicked ${count} times`; }, [count]); // Dependency array return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); }
The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so programs can alter the document structure, style, and content.
querySelector(), createElement(), appendChild(), classList, etc.addEventListener() and manage events like clicks, keyboard input.// Vanilla DOM example: Toggle a class on click const button = document.querySelector('#toggleBtn'); const element = document.getElementById('target'); button.addEventListener('click', () => { element.classList.toggle('highlight'); });
Explain how closures work in JavaScript and provide a practical use case.
function createCounter() { let count = 0; return { increment: () => ++count, getValue: () => count }; } const counter = createCounter(); console.log(counter.getValue()); // 0 counter.increment(); console.log(counter.getValue()); // 1
What is the purpose of the useEffect hook in React? How would you handle side effects and dependencies?
useEffect performs side effects in functional components (e.g., data fetching, subscriptions).
useEffect(callback, dependencies)[]: Run once after initial render.[dep]: Re-run when dep changes.useEffect(() => { fetchData(props.userId); return () => { /* Cleanup (e.g., unsubscribe) */ }; }, [props.userId]); // Only re-run if userId changes
How does React’s reconciliation algorithm work? What factors determine whether an element is updated?
<div> → <span>) trigger rebuilds.What are the differences between controlled and uncontrolled components in React?
defaultValue → value).defaultValue only; access via ref).// Controlled function Input() { const [value, setValue] = useState(''); return <input value={value} onChange={e => setValue(e.target.value)} />; } // Uncontrolled function Input() { const inputRef = useRef(null); return <input defaultValue="Initial" ref={inputRef} />; }
Why is direct DOM manipulation discouraged in React? What are the alternatives?
ReactDOM.createPortal) for modals/overlays.Create a useFetch hook that handles data fetching, loading states, and errors.
import { useState, useEffect } from 'react'; function useFetch(url) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchData = async () => { try { const response = await fetch(url); if (!response.ok) throw new Error('Network response was not ok'); const result = await response.json(); setData(result); } catch (err) { setError(err.message); } finally { setLoading(false); } }; fetchData(); }, [url]); return { data, loading, error }; } // Usage in component function UserProfile({ userId }) { const { data, loading, error } = useFetch(`https://api.example.com/users/${userId}`); if (loading) return <p>Loading...</p>; if (error) return <p>Error: {error}</p>; return <div>{data.name}</div>; }
useState for data, loading, and error states.url changes (dependency array).requestAnimationFrameAnimate a ball’s position smoothly without jitter.
function animateBall() { const ball = document.getElementById('ball'); let position = 0; function update() { position += 1; ball.style.left = `${position}px`; if (position < window.innerWidth - 50) { // Stop before edge requestAnimationFrame(update); } } update(); } // Trigger animation on button click document.getElementById('startBtn').addEventListener('click', animateBall);
Easy: Explain the output of the following closure code:
function test() { let count = 0; return function() { return ++count; }; } const t = test(); console.log(t()); console.log(t());
Hint: Understand how lexical scoping and closure retain access to
count.
Medium: Implement a React component that fetches and displays a random joke from an API, with error handling and a refresh button.
Hint: Use
useEffect,useState, anduseReffor cleanup/abort controller.
Intermediate: Build a vanilla JS function that efficiently adds thousand separators to a numeric string (e.g., 1234567 → 1,234,567) without using toLocaleString().
Hint: Use regular expressions or string manipulation in reverse.
requestAnimationFrame for animations.setCount(prev => prev + 1)).useEffect (causes infinite loops).Study Tip: Implement the practice problems manually, then compare your solution with optimized versions. Focus on readability and edge cases!
Start a new session to explore different topics or increase the difficulty level.
Start New Session