Skip to content

Interview questions by kuldeep

October 6, 2023 | 12:00 AM

Interview 101

main image

Table of Contents

Open Table of Contents

React

What is Reactjs?

React is a declarative, flexible open source front-end JavaScript library developed by Facebook in 2011. It follows the component-based approach for building reusable UI components, especially for single page application(SPA). It is used for developing interactive view layer of web and mobile apps. It was created by Jordan Walke, a software engineer at Facebook.

What are some of the key features of React?

The main features of React are:

Can web browsers read JSX directly?

What is the virtual DOM?

DOM stands for Document Object Model. The DOM represents an HTML document with a logical tree structure. Each branch of the tree ends in a node, and each node contains objects.

virtual dom

React keeps a lightweight representation of the real DOM in the memory, and that is known as the virtual DOM. When the state of an object changes, the virtual DOM changes only that object in the real DOM, rather than updating all the objects.

Synthetic events

In React, synthetic events are a way to handle and interact with browser events in a consistent and cross-browser-compatible manner. They are an abstraction layer on top of native browser events, provided by React to simplify event handling and provide a more predictable and unified API for working with events across different browsers.

Here are some key points about synthetic events in React:

Lifecycle methods

In React, the lifecycle of a component refers to the series of events or methods that a component goes through from its creation and mounting in the DOM to its updating, unmounting, and removal from the DOM.

Functional component vs class based components

In summary, functional components offer simplicity, ease of understanding, and improved performance compared to class-based components. They encourage a more declarative and functional programming style, making them the preferred choice for many React developers, especially in modern React applications.

Javascript

Closure

A closure is a fundamental concept in JavaScript (and many other programming languages) that refers to the ability of a function to “remember” its lexical scope even after that function has finished executing. In other words, a closure allows a function to maintain access to variables and parameters from its outer or enclosing function’s scope, even when the outer function has completed its execution.

function outerFunction() {
  const outerVar = "I am from the outer function";

  function innerFunction() {
    console.log(outerVar); // innerFunction has access to outerVar
  }

  return innerFunction;
}

const myClosure = outerFunction();
myClosure(); // Outputs: "I am from the outer function"

Closures play a significant role in React, as they enable various essential features and patterns in React applications. Here are some of the significant ways in which closures are used in React:

Closures are an integral part of React’s component architecture and state management. They enable components to encapsulate their internal state, handle events effectively, and maintain data privacy, which are crucial aspects of building scalable and maintainable React applications.

javascript for OOP

JavaScript is a multi-paradigm programming language, and it supports Object-Oriented Programming (OOP) concepts, including objects, classes (introduced in ES6), inheritance, encapsulation, polymorphism, and abstraction. While it has some differences from classical OOP languages, JavaScript’s OOP features make it versatile for object-oriented coding.

Asynchronous code in js

Asynchronous operations in JavaScript are crucial for performing tasks that may take some time to complete, such as making network requests, reading files, or executing long-running computations. Understanding how asynchronous operations work in JavaScript involves several key concepts: the call stack, callback queue (also known as the task queue), Web API, and the event loop.

Promise

A Promise in JavaScript is a special object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. Promises are commonly used for handling asynchronous code in a more structured and manageable way compared to callback functions.

Promises have several stages in their lifecycle, which are represented by three states: Pending, Fulfilled, and Rejected. Here’s an overview of each stage:

Here’s an example of a Promise in action:

const myPromise = new Promise((resolve, reject) => {
  // Simulate an asynchronous operation
  setTimeout(() => {
    const success = true; // Change to false to simulate a rejection
    if (success) {
      resolve("Operation successful");
    } else {
      reject("Operation failed");
    }
  }, 2000);
});

myPromise
  .then(result => {
    console.log("Fulfilled:", result);
  })
  .catch(error => {
    console.error("Rejected:", error);
  });

Promises are a fundamental tool for working with asynchronous code in JavaScript and are widely used to simplify complex asynchronous operations and improve code readability and maintainability. They provide a clear way to handle success and error scenarios while avoiding callback hell.

Strict mode

Strict mode is a development mode feature that helps you write cleaner and more reliable code by catching and highlighting potential problems in your application. It is not specific to React but is a JavaScript feature that can be applied to React applications (as well as other JavaScript code).

The main benefits of using strict mode in React are as follows:

It’s important to note that strict mode is intended for development use only. You should not use it in production as it can have a performance impact. When you build your application for production, strict mode-related code and checks are automatically stripped out.

Rehydration error

In React, rehydration errors can occur when there is a mismatch between the initial server-rendered HTML content and the subsequent client-side rendering. These errors can disrupt the intended behavior of your application and lead to issues. Here are some common causes of rehydration errors in React:

  1. Component Mismatch: If the component structure or hierarchy on the client-side differs from the server-rendered HTML, React might encounter problems when trying to reconcile the differences.

  2. Data Mismatch: If the data used to render components on the client doesn’t match the data used for server-side rendering, you can run into issues. This can happen if the data fetch or API calls on the client differ from those on the server.

  3. Inconsistent State: If there’s a discrepancy in the state of a component between the server and client, React might not be able to rehydrate the component correctly. This could be due to differences in component state or props.

  4. Misconfigured Routing: Issues with client-side routing can lead to rehydration errors. If the server renders one route, but the client attempts to render a different route, problems can occur.

  5. Differences in Event Handlers: If you have event handlers or other side effects that behave differently on the server and client, this can cause rehydration errors.

  6. Third-party Libraries: Third-party libraries and components that are not designed with server-side rendering in mind can introduce rehydration problems.

  7. Async Operations: If there are asynchronous operations that depend on client-side data and they don’t execute in the same order as on the server, this can cause issues.

To avoid rehydration errors, it’s essential to ensure consistency between the initial server-rendered HTML and the client-side rendering. This can involve using strategies like code splitting, ensuring that your data fetching is consistent, and handling client-side routing appropriately. Additionally, consider server-side rendering frameworks like Next.js that simplify this process and reduce the chances of rehydration errors.