How do you organize code in a JavaScript project?
The short answer
Good code organization groups related files together, separates concerns, and makes things easy to find. The most common approaches are: organizing by feature (all files for a feature in one folder), organizing by type (components, hooks, utils in separate folders), or a mix of both. The right structure depends on the project size.
By feature (recommended for larger projects)
1src/2 features/3 auth/4 LoginForm.jsx5 useAuth.js6 authApi.js7 auth.test.js8 dashboard/9 Dashboard.jsx10 DashboardStats.jsx11 useDashboardData.js12 products/13 ProductList.jsx14 ProductCard.jsx15 productApi.js
Everything related to authentication lives in auth/. You do not have to jump between folders to work on a feature.
By type (common for smaller projects)
1src/2 components/3 LoginForm.jsx4 Dashboard.jsx5 ProductList.jsx6 hooks/7 useAuth.js8 useDashboardData.js9 api/10 authApi.js11 productApi.js12 utils/13 formatDate.js14 formatCurrency.js
Simple and works well for small to medium projects. Gets harder to navigate as the project grows.
Common conventions
- One component per file — easier to find and test
- Index files for clean imports —
import { Button } from './components' - Colocate tests — put
Component.test.jsnext toComponent.jsx - Shared utilities in
utils/— formatters, helpers, constants - Keep files small — if a file is over 200-300 lines, consider splitting it
In React specifically
1src/2 components/ — shared, reusable components3 pages/ — route-level components4 hooks/ — custom hooks5 utils/ — helper functions6 api/ — API client and request functions7 constants/ — shared constants8 types/ — TypeScript types
Interview Tip
Describe both approaches (by feature and by type) and say when each makes sense. Mentioning that you colocate tests and keep files small shows you care about maintainability. This is an opinion question — have a preference and explain why.
Why interviewers ask this
This tests how you think about code architecture. Interviewers want to see if you can organize a project so other developers can navigate it easily. Good organization shows experience building and maintaining real projects.