Posts

React interviews questions and answers with code

Image
React Interview Preparation: Best Questions, Best Answers, and Sample Code 1. What are the differences between functional and class components in React? Answer : Functional components are simpler and mainly focus on rendering UI, while class components provide more powerful features like state management and lifecycle methods. In recent versions of React, hooks ( useState ,  useEffect ) enable functional components to use state and lifecycle features without needing to be classes. // Functional Component function FunctionalComponent(props) {     const [count, setCount] = useState(0);     return (         <div>             <p>Count: {count}</p>             <button onClick={() => setCount(count + 1)}>Increment</button>         </div>     ); } // Class Component class ClassComponent extends React.Component {     constructor(props) {         super(props);         this.state = { count: 0 };     }     increment = () => {         this.setState({ count

JavaScript interview questions & answers with code

Image
Top JavaScript Interview Questions and Expert Answers with Code Examples 1. Flatten a Nested Object Question : Write a function to flatten a nested JavaScript object. Code : function flattenObject(obj, prefix = '', res = {}) {   for (let key in obj) {     const newKey = prefix ? `${prefix}.${key}` : key;     if (typeof obj[key] === 'object' && obj[key] !== null) {       flattenObject(obj[key], newKey, res);     } else {       res[newKey] = obj[key];     }   }   return res; } console.log(flattenObject({ a: { b: { c: 1 } }, d: 2 })); // Output: { 'a.b.c': 1, d: 2 } 2. Find Duplicates in an Array Question : Write a function to find duplicate values in an array. Code : function findDuplicates(arr) {   const counts = {};   return arr.filter(item => counts[item] ? true : (counts[item] = 1, false)); } console.log(findDuplicates([1, 2, 2, 3, 4, 4, 5])); // Output: [2, 4] 3. Implement debounce Question : Write a debounce function that limits the rate a functi