1. What are the differences between .then()
and async/await
in handling asynchronous operations in JavaScript?
Use .then()
for promise chaining and async/await
to make asynchronous code look synchronous, improving readability and error handling.
2. What are the lifecycle methods in React components, and how are they used?
Lifecycle methods like componentDidMount()
and hooks like useEffect()
are used for performing side effects such as data fetching after the initial render.
3. How does CORS enhance security, and how can it be managed?
CORS restricts web applications from requesting resources from different domains; manage it by configuring the server to include appropriate CORS headers like Access-Control-Allow-Origin
.
4. When should you use useState
vs. useReducer
in React?
Use useState
for simple state logic and useReducer
for complex state logic that involves multiple sub-values or when the next state depends on the previous one.
5. What is the virtual DOM, and how does it work in React?
The virtual DOM is a lightweight copy of the real DOM, used by React to optimize rendering by updating only changed elements.
6. How do you handle state management in a React application?
Use state management libraries like Redux or Context API for managing global state across components.
7. What is the purpose of prop drilling, and how can it be avoided?
Prop drilling passes data through multiple levels of components; avoid it by using Context API or state management libraries.
8. Explain the concept of higher-order components (HOCs) in React.
HOCs are functions that take a component and return a new component, enhancing it with additional functionality.
9. What are CSS preprocessors, and why are they useful?
CSS preprocessors like Sass and Less extend CSS with variables, nesting, and mixins, making stylesheets more maintainable.
10. How do you optimize the performance of a React application?
Optimize performance by using techniques like code splitting, lazy loading, memoization, and avoiding unnecessary re-renders.
11. What are Webpack and Babel, and how are they used in front-end development?
Webpack is a module bundler, and Babel is a JavaScript compiler; both are used to transform and bundle code for deployment.
12. Explain the box model in CSS and its components.
The box model consists of content, padding, border, and margin, defining the layout and spacing of elements.
13. How do media queries work in CSS for responsive design?
Media queries apply styles based on device characteristics like width and height, enabling responsive design.
14. What are the different types of positioning in CSS?
Positioning types include static, relative, absolute, fixed, and sticky, each affecting element layout differently.
15. How do you create a grid layout using CSS Grid?
Use CSS Grid properties like grid-template-columns
and grid-template-rows
to define a grid layout.
16. What is flexbox and its key properties?
Flexbox is a layout model for distributing space along a single row or column; key properties include flex-direction
, justify-content
, and align-items
.
17. What are the differences between localStorage
, sessionStorage
, and cookies?
localStorage
and sessionStorage
store data in the browser with different lifespans, while cookies store data that can be sent to the server.
18. How do you implement form validation in JavaScript?
Implement form validation using HTML5 validation attributes and JavaScript for custom validation logic.
19. What is the purpose of a polyfill in web development?
Polyfills add support for modern JavaScript features in older browsers that do not natively support them.
20. How do you manage dependencies in a front-end project?
Manage dependencies using package managers like npm or Yarn, and define them in package.json
.