React Questions
React Questions
Answer:
Answer:
A closure is a function that retains access to variables from its lexical scope even after
the function that created those variables has finished executing.
Answer:
Hoisting is JavaScript's default behavior of moving all declarations (var, function, etc.)
to the top of their scope during compilation. Variables declared with var are hoisted and
initialized with undefined, while let and const are hoisted but remain uninitialized.
Answer:
Answer:
JavaScript has primitive types (String, Number, Boolean, Undefined, Null, Symbol,
BigInt) and reference types (Object, Array, Function, Date, etc.).
Answer:
this refers to the context in which a function is called. It could refer to the global object
(window in browsers), an object instance, or undefined in strict mode.
7. What are promises in JavaScript?
Answer:
Answer:
Answer:
Event delegation is a technique where you attach a single event listener to a parent
element to handle events for multiple child elements, using event bubbling.
Answer:
Answer:
Answer:
bind() creates a new function that, when called, has its this keyword set to the provided
value, and it can be pre-configured with arguments.
Every JavaScript object has a prototype, which is another object from which it can
inherit properties and methods. It enables inheritance in JavaScript.
14. What are arrow functions, and how are they different from regular functions?
Answer:
Arrow functions are a shorter syntax for defining functions. They do not have their own
this value and instead inherit this from their surrounding scope.
15. What is the purpose of the async and await keywords in JavaScript?
Answer:
async makes a function return a promise, and await pauses execution until the
promise is resolved, making asynchronous code easier to read.
Answer:
Answer:
setTimeout() sets a timer to execute a function after a specified delay (in milliseconds).
Answer:
• Function declarations are hoisted, so they can be called before they are defined.
• Function expressions are not hoisted and must be defined before being called.
Answer:
Default parameters allow you to assign default values to function parameters if they
are not provided when the function is called.
20. What is the difference between synchronous and asynchronous code in
JavaScript?
Answer:
Answer:
Answer:
Debouncing is a technique used to limit the rate at which a function is executed, often
used to handle events like keypresses or window resizing.
Answer:
Answer:
typeof returns a string indicating the type of the operand (e.g., "string", "number",
"boolean").
Answer:
const obj = {
[sym1]: 'value1',
[sym2]: 'value2'
};
-----------------------------------------------------------------------------------------------------------
React JS Interview Questions
Answer:
React is a JavaScript library for building user interfaces, particularly for single-page
applications. It allows developers to build reusable UI components and efficiently
update the UI when data changes.
Answer:
JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML-
like code inside JavaScript. It gets transpiled to JavaScript at runtime.
Answer:
Components are the building blocks of a React application. They allow you to
encapsulate UI elements and logic into reusable units. Components can be functional
or class-based.
31. What is the difference between functional and class components in React?
Answer:
Answer:
React Hooks are functions that let you "hook into" React state and lifecycle features
from functional components. Examples include useState, useEffect, and useContext.
Answer:
useState is a React hook that allows you to add state to functional components. It
returns an array with the current state and a function to update that state.
Answer:
useEffect is a React hook that allows you to perform side effects in functional
components (e.g., fetching data, setting up subscriptions). It replaces lifecycle
methods like componentDidMount, componentDidUpdate, and
componentWillUnmount.
Answer:
The Virtual DOM is a lightweight in-memory representation of the actual DOM. React
uses it to optimize updates by comparing changes in the Virtual DOM with the real DOM
and updating only the necessary parts.
Answer:
The key prop is used to uniquely identify elements in a list. It helps React efficiently
update and re-render lists by distinguishing which items have changed.
Answer:
Answer:
The Context API is a React feature that allows you to share data between components
without passing props manually through every level of the component tree.
Answer:
The render() method is required in class components. It returns the JSX that defines
what the UI should look like. In functional components, you directly return the JSX from
the function.
Answer:
An HOC is a function that takes a component and returns a new component with
additional props or functionality. It’s used to reuse component logic.
Answer:
React Router is a library for handling routing in React applications, allowing navigation
between different views or pages in a single-page application.
Answer:
Redux is a state management library that helps manage the state of an application in a
predictable way. It’s often used in React applications to store the state of the app
centrally and to make the state accessible to any component.
Answer:
• Props: Immutable and passed from parent to child components. They are used
to pass data to components.
• State: Mutable and local to the component. It can be changed and used to
trigger re-renders.
Answer:
Fragments are used to group multiple elements without adding extra nodes to the
DOM. You can use <React.Fragment> or shorthand <> to wrap elements without
creating a parent DOM node.
Answer:
useRef is a React hook that allows you to persist values across renders without causing
re-renders. It can also be used to reference DOM elements directly.
Answer:
• useEffect can run after every render, and you can control when it runs using
dependencies.
• componentDidMount is a class component lifecycle method that runs once
after the component mounts.
Answer:
Answer:
Answer:
StrictMode is a wrapper component that helps identify potential problems in the
application, such as unsafe lifecycle methods, legacy API usage, or potential bugs. It
only runs in development mode and does not affect production builds.