0% found this document useful (0 votes)
34 views

Interview Prep - Frontend

Uploaded by

Indumathy prem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Interview Prep - Frontend

Uploaded by

Indumathy prem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

1. What is Hoisting in JS?

Hoisting is a JavaScript behavior where variable and function


declarations are moved to the top of their containing scope during the compilation
phase. This means that you can use a variable or function before it's declared in your
code.

2. How does Hoisting work? JavaScript's hoisting works by moving variable


declarations to the top of their scope, while the assignments remain in place. This allows
you to use variables before they're formally declared in the code.

3. What is the difference between let and var? The main difference between let and
var is their scope. Variables declared with var have function scope or global scope,
whereas variables declared with let have block scope. This means that let is confined
to the block it's declared in, like loops or conditionals.

4. What is the Event Loop? The Event Loop is a fundamental concept in JavaScript's
concurrency model. It's responsible for handling asynchronous operations and ensuring
that they don't block the main thread. It continuously checks the message queue for
tasks to execute.

5. What is the precedence in the Event Loop? The Event Loop follows a specific order
of execution: First, it processes tasks in the main queue, then checks the microtask
queue. Microtasks are usually higher-priority tasks that are executed before the next
cycle of the Event Loop.

6. What is the difference between setTimeout and setInterval? Both setTimeout


and setInterval are used to execute code after a specific delay. The difference is that
setTimeout executes the code once after the delay, while setInterval repeatedly
executes the code at intervals specified by the delay.

7. Where do you use the Rest Operator? The Rest Operator (...) is used in function
parameters to collect all remaining arguments into an array. It's particularly useful when
you have a variable number of arguments and want to work with them as an array.

8. Have you heard of array.reverse? Yes, array.reverse is a built-in JavaScript array


method that reverses the order of elements in an array. The first element becomes the
last, and the last becomes the first.

9. What is meant by Shallow copy and Deep copy? Shallow copy and deep copy refer
to ways of copying objects. A shallow copy creates a new object but does not deeply
copy nested objects within it. A deep copy creates a new object and recursively copies
all nested objects within it as well.

10. What are Closures? Closures are a JavaScript feature that allows functions to
remember the variables from the scope in which they were created, even if that scope
has finished executing. They are often used to create private variables and data
encapsulation.

11. Have you used the reduce function in JS? Yes, the reduce function is used to
iterate over an array and accumulate a single value based on the elements of the array.

12. What is the difference between map and reduce? map is used to transform each
element of an array and returns a new array with the same number of elements. reduce
is used to accumulate a single value by iteratively processing the elements of the array.

13. What parameters does the map function accept? The map function accepts a
callback function as its parameter, which is applied to each element of the array. The
callback can also take additional parameters: the current element, the current index, and
the array itself.

14. What is the difference between a Promise and a Callback? Both Promises and
callbacks are used for handling asynchronous operations. However, Promises provide a
more structured and readable way to handle asynchronous code, while callbacks can
lead to callback hell and complex nesting.

15. What CSS position attributes have you used? The CSS position attributes I've
used include static, relative, absolute, and fixed.

16. What is the difference between them?

 static: Default positioning, elements follow the normal flow of the document.
 relative: Positioned relative to its normal position, other elements are not affected.
 absolute: Positioned relative to its nearest positioned ancestor, if any. It's removed from
the normal flow.
 fixed: Positioned relative to the viewport, stays in the same position even when
scrolling.

17. What is Flexbox? Flexbox (Flexible Box Layout) is a CSS layout model designed to
create flexible and efficient layouts. It simplifies the alignment, distribution, and order of
elements within a container, especially when dealing with dynamic and responsive
designs.

18. What is the difference between display=none and visibility=hidden?


display: none completely removes the element from the document flow, making it
invisible and not taking up space. visibility: hidden makes the element invisible, but
it still occupies space in the document flow.

19. What Hooks have you used? I've used various React Hooks, including useState,
useEffect, useContext, useReducer, useCallback, and useMemo.

20. What is the purpose of useCallback? useCallback is used to memoize functions


in React. It optimizes performance by ensuring that a function instance is not recreated
on every render, especially when passed as a prop to child components.

21. What are Class-based Lifecycle methods? Class-based components in React have
several lifecycle methods like componentDidMount, componentDidUpdate, and
componentWillUnmount. These methods allow you to perform actions when a
component is created, updated, or removed from the DOM.

22. How would you achieve componentDidMount, componentDidUpdate, and


componentWillUnmount in a functional component? You would use the useEffect
hook to achieve these functionalities. The effect function passed to useEffect can
handle different scenarios by using the dependency array and cleanup function.

23. What are Pure Components and their purpose? Pure Components are
components that only re-render when their props or state change. They help optimize
performance by reducing unnecessary renders and updates.

24. What are Higher Order Components (HOCs)? Higher Order Components are a
pattern in React where a function takes a component and returns a new component
with enhanced behavior. They're used for code reuse, cross-cutting concerns, and
adding functionality to components.

25. Have you used the Context API? Yes, the Context API is used for managing state
that needs to be accessible by multiple components without prop drilling.

26. If you already have state management in React, why go for Redux? Redux is
typically used for more complex state management scenarios where the built-in React
state management might become difficult to manage, especially in large applications
with multiple components sharing the same state.
27. How does Redux work? Redux centralizes application state in a single store.
Actions are dispatched to modify the state using pure reducer functions. Components
can subscribe to the store and update UI when the state changes.

28. Have you used any Middlewares? Yes, Redux middleware intercepts dispatched
actions before they reach the reducer, allowing you to add custom behavior. Examples
include redux-thunk for handling asynchronous actions and redux-logger for logging.

29. What is the purpose of using middlewares? Middlewares enhance the


functionality of Redux by adding custom logic to the action dispatching process. They're
often used for handling asynchronous actions, logging, routing, and more.

The spread (...) and rest (...) operators are both introduced in JavaScript (ES6) and are used
for working with arrays and objects. They look similar but are used in different contexts and
have distinct purposes.

Spread Operator (...)

The spread operator is used to split an array or object into individual elements or key-value pairs.
It's often used to create copies of arrays or objects or to merge arrays and objects together.

1. Copying Arrays:
javascriptCopy code
const originalArray = [1, 2, 3]; const copiedArray = [...originalArray]; // Creates a shallow copy
2. Merging Arrays:
javascriptCopy code
const array1 = [1, 2, 3]; const array2 = [4, 5, 6]; const mergedArray = [...array1, ...array2];
3. Copying Objects:
javascriptCopy code
const originalObject = { name: "John", age: 30 }; const copiedObject = { ...originalObject }; // Creates a shallow
copy
4. Merging Objects:
javascriptCopy code
const object1 = { a: 1, b: 2 }; const object2 = { b: 3, c: 4 }; const mergedObject = { ...object1, ...object2 };

Rest Operator (...)

The rest operator is used to collect multiple elements or parameters into a single array or object.
It's often used in function arguments to handle a variable number of arguments.

1. Collecting Remaining Array Elements:


javascriptCopy code
const [first, second, ...rest] = [1, 2, 3, 4, 5]; // first = 1, second = 2, rest = [3, 4, 5]
2. Collecting Remaining Function Arguments:
javascriptCopy code
function sum(...numbers) { return numbers.reduce((acc, num) => acc + num, 0); } const result = sum(1, 2, 3, 4, 5); //
result = 15
3. Collecting Remaining Object Properties (Rest in Objects):
javascriptCopy code
const { a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 }; // a = 1, b = 2, rest = { c: 3, d: 4 }

In summary, the spread operator is used to break apart arrays or objects, while the rest operator is
used to collect multiple values into an array or object. They are both powerful tools for working
with data in JavaScript.
The spread ( ...) and rest ( ...) operators are both introduced in JavaScript (ES6) and are used
for working with arrays and objects. They look similar but are used in different contexts and
have distinct purposes.

Spread Operator (...)

The spread operator is used to split an array or object into individual elements or key-
value pairs. It's often used to create copies of arrays or objects or to merge arrays and
objects together.

1. Copying Arrays:
const originalArray = [1, 2, 3];

const copiedArray = [...originalArray]; // Creates a shallow copy

Merging Arrays:

const array1 = [1, 2, 3];

const array2 = [4, 5, 6];

const mergedArray = [...array1, ...array2];

Copying Objects:

const originalObject = { name: "John", age: 30 };

const copiedObject = { ...originalObject }; // Creates a shallow copy

Merging Objects:

const object1 = { a: 1, b: 2 };

const object2 = { b: 3, c: 4 };

const mergedObject = { ...object1, ...object2 };


Rest Operator (...)

The rest operator is used to collect multiple elements or parameters into a single array
or object. It's often used in function arguments to handle a variable number of
arguments.

1. Collecting Remaining Array Elements:


const [first, second, ...rest] = [1, 2, 3, 4, 5];

// first = 1, second = 2, rest = [3, 4, 5]

Collecting Remaining Function Arguments:

function sum(...numbers) {

return numbers.reduce((acc, num) => acc + num, 0);

const result = sum(1, 2, 3, 4, 5); // result = 15

Collecting Remaining Object Properties (Rest in Objects):

const { a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 };

// a = 1, b = 2, rest = { c: 3, d: 4 }

In summary, the spread operator is used to break apart arrays or objects, while the rest operator
is used to collect multiple values into an array or object. They are both powerful tools for
working with data in JavaScript.

`var`, `let`, and `const` are three different ways to declare variables in JavaScript, each with its own
scope and behavior.

1. **`var`**:

`var` was traditionally used to declare variables in JavaScript before the introduction of `let` and
`const`. Variables declared with `var` are function-scoped, which means they are only accessible within
the function where they are declared or globally if declared outside of any function. One key
characteristic of `var` is that it's subject to hoisting, meaning the variable declaration is moved to the top
of its scope during the compilation phase.
```javascript

function example() {

var x = 10;

if (true) {

var x = 20; // This reassigns the same variable

console.log(x); // Outputs 20

console.log(x); // Outputs 20 (the same variable is being used)

```

2. **`let`**:

`let` was introduced in ECMAScript 6 (ES6) and provides block-scoping. Variables declared with `let` are
confined to the block (enclosed by curly braces) where they are defined. This helps prevent unintended
variable reassignments and is generally considered better practice compared to `var`.

```javascript

function example() {

let y = 10;

if (true) {

let y = 20; // This creates a new variable y within this block

console.log(y); // Outputs 20

console.log(y); // Outputs 10 (the outer variable is unaffected)

```
3. **`const`**:

`const` also came with ES6 and is used to declare variables that are meant to be constant, i.e., their
values cannot be reassigned after they are defined. Like `let`, `const` is block-scoped.

```javascript

function example() {

const z = 10;

if (true) {

const z = 20; // This creates a new constant z within this block

console.log(z); // Outputs 20

console.log(z); // Outputs 10 (the outer constant is unaffected)

```

In modern JavaScript development, it's recommended to use `let` for variables that can change and
`const` for variables that should remain constant. The usage of `var` is generally discouraged due to its
potential for unintended scoping and hoisting issues.

Of course! Here are the CSS interview questions along with their answers:

1. **What is CSS, and what is its purpose in web development?**

Answer: CSS stands for Cascading Style Sheets. Its purpose in web development is to control the
presentation and layout of web pages, allowing developers to define how HTML elements should be
displayed, including aspects like colors, fonts, spacing, and positioning.
2. **Explain the difference between inline, internal, and external CSS.**

Answer:

- **Inline CSS:** Styles applied directly to an HTML element using the `style` attribute.

- **Internal CSS:** Styles defined within a `<style>` tag in the HTML document's `<head>`.

- **External CSS:** Styles stored in a separate .css file and linked to the HTML document using the
`<link>` tag.

3. **What are selectors in CSS? Provide examples of different types of selectors.**

Answer: Selectors are patterns used to select and apply styles to HTML elements. Examples include:

- **Element Selector:** Selects all instances of a specific HTML element (e.g., `p { color: blue; }`).

- **Class Selector:** Selects elements with a specific class (e.g., `.my-class { font-size: 16px; }`).

- **ID Selector:** Selects a single element with a specific ID (e.g., `#my-id { background-color:
yellow; }`).

4. **How does the CSS box model work? Describe its components.**

Answer: The CSS box model defines an element's layout. It consists of four components:

- **Content:** The actual content or text inside the element.

- **Padding:** The space between the content and the element's border.

- **Border:** A visible or invisible border around the padding.

- **Margin:** The space between the border and neighboring elements.

5. **What is the importance of specificity in CSS, and how is it calculated?**


Answer: Specificity determines which CSS rule takes precedence when multiple rules target the same
element. It's calculated using a value system, with elements, classes, and IDs assigned specific values.
The more specific the selector, the higher its priority.

6. **Explain the concept of the CSS cascade and the order of importance of CSS rules.**

Answer: The CSS cascade refers to the process of determining which styles to apply when multiple
conflicting rules exist. It follows this order of importance:

- **Important Rule:** Styles marked with `!important` have the highest priority.

- **Specificity:** More specific selectors override less specific ones.

- **Source Order:** The last rule encountered in the CSS file takes precedence.

7. **What are pseudo-classes and pseudo-elements in CSS? Provide examples.**

Answer: Pseudo-classes and pseudo-elements allow you to select elements based on their state or
position. Examples:

- **Pseudo-class:** `:hover` selects an element when the mouse is over it (e.g., `a:hover { color:
red; }`).

- **Pseudo-element:** `::before` creates a virtual element before the content of an element (e.g.,
`p::before { content: "- "; }`).

8. **How can you center an element horizontally and vertically using CSS?**

Answer: To center an element both horizontally and vertically, you can use Flexbox or Grid layouts. For
example, with Flexbox:

```css

.container {
display: flex;

justify-content: center;

align-items: center;

```

These answers should give you a good starting point for understanding these CSS concepts. Remember
to practice and adapt your answers based on your experience and the specific requirements of your
interview.

Callbacks and Promises are two common ways to handle asynchronous operations in JavaScript. They
help you manage the flow of your code when dealing with tasks that may take some time to complete,
such as reading a file, making an HTTP request, or any other asynchronous operation.

Here's an example of using both callbacks and Promises to fetch data from a hypothetical API.

**Using Callbacks:**

```javascript

// Simulate an asynchronous API call

function fetchDataWithCallback(callback) {

setTimeout(function () {

const data = { message: "Data fetched with Callbacks" };

callback(data);

}, 1000);

// Example of using the callback


function processCallbackData(data) {

console.log(data.message);

fetchDataWithCallback(processCallbackData);

console.log("Fetching data with Callbacks...");

```

In this example, `fetchDataWithCallback` simulates an asynchronous operation, and when it's done, it
calls the provided callback function (`processCallbackData`) with the fetched data.

**Using Promises:**

```javascript

// Simulate an asynchronous API call using Promises

function fetchDataWithPromise() {

return new Promise(function (resolve, reject) {

setTimeout(function () {

const data = { message: "Data fetched with Promises" };

resolve(data); // Success

// If there's an error, you can use reject(error) instead of resolve

}, 1000);

});

// Example of using the Promise


fetchDataWithPromise()

.then(function (data) {

console.log(data.message);

})

.catch(function (error) {

console.error("Error:", error);

});

console.log("Fetching data with Promises...");

```

In this example, `fetchDataWithPromise` returns a Promise that either resolves with the fetched data or
rejects with an error if something goes wrong. You can use `.then()` to handle the success case and
`.catch()` to handle errors.

Promises provide a more structured and readable way to handle asynchronous code compared to
callbacks, especially when dealing with complex chains of asynchronous operations. However, modern
JavaScript also introduced `async/await`, which is even more convenient for handling Promises.

Here's an example using `async/await` with Promises:

```javascript

// Simulate an asynchronous API call using Promises

function fetchDataWithPromise() {

return new Promise(function (resolve, reject) {

setTimeout(function () {

const data = { message: "Data fetched with Promises and async/await" };


resolve(data); // Success

// If there's an error, you can use reject(error) instead of resolve

}, 1000);

});

// Example of using async/await with Promises

async function fetchDataAsync() {

try {

const data = await fetchDataWithPromise();

console.log(data.message);

} catch (error) {

console.error("Error:", error);

console.log("Fetching data with Promises and async/await...");

fetchDataAsync();

```

This example demonstrates how you can use `async/await` to write asynchronous code in a more
synchronous-looking style while still leveraging Promises for the underlying asynchronous behavior.

localStorage and sessionStorage are two web storage mechanisms in JavaScript that allow you to store
key-value pairs in a web browser. They are similar in purpose but have different lifespans and use cases:

1. **localStorage:**
- **Lifespan:** Data stored in localStorage persists even after the browser is closed and reopened. It
remains until explicitly removed or cleared.

- **Scope:** Data in localStorage is available across browser tabs and windows from the same
domain.

- **Size:** localStorage provides more storage space (typically around 5-10 MB) compared to
sessionStorage.

Example usage:

```javascript

// Storing data in localStorage

localStorage.setItem('key', 'value');

// Retrieving data from localStorage

const value = localStorage.getItem('key');

// Removing data from localStorage

localStorage.removeItem('key');

```

2. **sessionStorage:**

- **Lifespan:** Data stored in sessionStorage is only available for the duration of the page session. It is
cleared when the page is closed or refreshed.

- **Scope:** sessionStorage is limited to the specific tab or window that created it. It is not accessible
in other tabs or windows.

- **Size:** sessionStorage also provides a similar amount of storage space as localStorage.

Example usage:
```javascript

// Storing data in sessionStorage

sessionStorage.setItem('key', 'value');

// Retrieving data from sessionStorage

const value = sessionStorage.getItem('key');

// Removing data from sessionStorage

sessionStorage.removeItem('key');

```

When to use localStorage vs. sessionStorage depends on your application's requirements. Use
localStorage for data that should persist across sessions and is shared among different tabs or windows
of your website. Use sessionStorage for data that should only be available during the current page
session and is isolated to the specific tab or window.

REDUX

Certainly, I can provide you with some common interview questions and answers related to Redux.
Remember to adapt your responses based on your own experiences and understanding of Redux. Here
are a few questions and sample answers:

1. **What is Redux, and why would you use it in a web application?**

**Answer:** Redux is a predictable state container for JavaScript applications. It's used to manage the
application's state in a centralized and predictable manner. Redux is beneficial in large or complex web
applications to ensure a single source of truth for data, simplify state changes, and make debugging
easier.

2. **What are the core principles of Redux?**


**Answer:** Redux follows three core principles:

- Single source of truth: The entire state of an application is stored in a single JavaScript object.

- State is read-only: The only way to change the state is by dispatching actions, which describe what
happened.

- Changes are made through pure functions: Reducers are pure functions that specify how the
application's state changes in response to actions.

3. **Explain the main components of Redux.**

**Answer:** Redux has four main components:

- **Store**: It holds the state of the application.

- **Actions**: These are plain JavaScript objects that describe what happened.

- **Reducers**: Reducers are functions that specify how the application's state changes in response to
actions.

- **Dispatch**: It's a method used to send actions to the store, which triggers state updates.

4. **What is an action creator in Redux?**

**Answer:** An action creator is a function that returns an action object. It simplifies the process of
creating and dispatching actions in Redux. For example, if you want to update the state to add an item,
you can use an action creator to generate an action object with the necessary information.

5. **Explain the concept of middleware in Redux.**

**Answer:** Middleware in Redux provides a way to add custom functionality to the dispatch process.
It allows you to intercept, modify, or perform asynchronous actions before the action reaches the
reducer. Common middleware includes `redux-thunk` for handling asynchronous actions and `redux-
logger` for logging state changes.
6. **What is the purpose of selectors in Redux?**

**Answer:** Selectors are functions used to extract specific pieces of data from the Redux store. They
help in isolating the structure of the state from the components, making it easier to maintain and test
the application. Selectors also improve performance by memoizing the results.

7. **How does Redux differ from React's built-in state management?**

**Answer:** Redux is an external library for managing state, while React has its built-in state
management. Redux is more suitable for complex applications where state needs to be shared among
components, whereas React's state is typically used for local component state.

These questions and answers should give you a good starting point for preparing for a Redux-related
interview. Remember to provide examples from your own projects if applicable, as practical experience
is often highly valued in interviews.

FLUX:

Flux is an architectural pattern for building user interfaces, particularly in web applications. It was
developed by Facebook and is often used in conjunction with React, although it can be applied to other
libraries and frameworks as well. The primary goal of Flux is to manage the flow of data in a
unidirectional and predictable manner. Here's an explanation of the key components and concepts of
Flux:

1. **Action**: Actions are simple JavaScript objects that represent events or user interactions in an
application. They contain a `type` field that describes the action and may include additional data. Actions
are typically created and dispatched by components when something happens, such as a button click or
an API response.
2. **Dispatcher**: The Dispatcher is a central hub that receives actions and dispatches them to
registered stores. It ensures that actions are processed in a consistent order and prevents race
conditions. Multiple stores can listen to the dispatcher and respond to actions independently.

3. **Store**: Stores are responsible for managing the application's data and state. They register with
the dispatcher and specify how they should respond to specific types of actions. When a store handles
an action, it updates its data and emits a change event to notify the views that the data has changed.

4. **View**: Views represent the user interface components in the application. They subscribe to
changes in stores and update themselves when the stores emit change events. Views are typically React
components in a React-Flux application, and they retrieve data from stores to render the user interface.

5. **Action Creator**: Action creators are helper functions that create and return action objects. They
encapsulate the logic for generating actions based on user interactions or other events. Action creators
are often used in conjunction with views to trigger actions.

6. **Unidirectional Data Flow**: In Flux, data flows in a single direction, from the views to the actions,
through the dispatcher to the stores, and finally back to the views. This unidirectional flow ensures
predictability and makes it easier to debug and reason about how data changes in an application.

7. **Immutable Data**: Flux encourages the use of immutable data structures. When a store updates
its data, it typically creates a new copy of the data rather than modifying the existing data. This helps
prevent unintended side effects and makes it easier to track changes.

Flux provides a structured and organized way to manage the state of an application, particularly in
situations where the state is complex or needs to be shared across multiple components. While Redux,
which I mentioned earlier, is inspired by Flux and follows similar principles, it has become more popular
in the React community due to its simplicity and extensive ecosystem. However, Flux remains a valuable
pattern to understand, especially for historical context and when working with frameworks other than
React.

ASYNCHRONUS:
Asynchronous refers to a type of operation or behavior in computing and programming where tasks
don't necessarily occur in a strict sequential order. Instead, asynchronous operations allow multiple
tasks to overlap and execute independently, without waiting for the completion of each other. This
approach is particularly useful for tasks that might take some time to complete, such as fetching data
from a server, reading/writing files, or waiting for user input, without causing the entire program to
block or freeze.

Here are some key characteristics of asynchronous operations:

1. **Non-blocking**: Asynchronous operations don't block the execution of the program. Instead of
waiting for one task to finish before starting the next, the program can continue to execute other tasks
while the asynchronous operation runs in the background.

2. **Callback Functions**: Asynchronous operations often use callback functions to handle the result or
response once the operation is complete. These callback functions are provided to the asynchronous
function as arguments and are executed when the operation finishes.

3. **Concurrency**: Asynchronous operations allow for better concurrency, meaning multiple tasks can
progress simultaneously. This can lead to more efficient use of system resources and improved
responsiveness in applications.

4. **Event-driven**: Many asynchronous operations are event-driven, meaning they respond to events
or triggers, such as user actions, timers, or data arrivals. When the event occurs, the associated
asynchronous operation is initiated.

5. **Promises and Async/Await**: In modern JavaScript, promises and the async/await syntax provide a
more structured and readable way to work with asynchronous code. Promises represent the eventual
result of an asynchronous operation, and async/await allows you to write asynchronous code in a more
synchronous style.

6. **Error Handling**: Asynchronous code often requires careful error handling since exceptions that
occur in asynchronous tasks might not propagate in the same way as synchronous code. Proper error
handling is crucial to maintain the stability of an application.
Common use cases for asynchronous operations include making network requests, reading/writing to
databases, handling user input, and performing time-consuming computations without blocking the
main thread of an application.

In summary, asynchronous operations allow programs to perform tasks concurrently and handle
situations where operations take time to complete without causing the entire program to hang or
become unresponsive. This is a fundamental concept in modern software development, especially in
web and mobile applications.

You might also like