Logo

dev-resources.site

for different kinds of informations.

JavaScript interview questions & answers with code

Published at
12/21/2024
Categories
discuss
javascript
programming
atharvgyan
Author
atharvgyan
Author
10 person written this
atharvgyan
open
JavaScript interview questions & answers with code

Top JavaScript Interview Questions and Expert Answers with Code Examples

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 }
Enter fullscreen mode Exit fullscreen mode

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]

Enter fullscreen mode Exit fullscreen mode

3. Implement debounce
Question : Write a debounce function that limits the rate a function is called.

Code :

function debounce(func, delay) {

** let timer;**

** return function (...args) {**

** clearTimeout(timer);**

** timer = setTimeout(() => func.apply(this, args), delay);**

** };**

}

const log = debounce(() => console.log('Logged after 1s'), 1000);

log();

Enter fullscreen mode Exit fullscreen mode

4. Reverse a String Recursively

Question : Write a recursive function to reverse a string.

Code :

function reverseString(str) {

** if (str === "") return "";**

** return reverseString(str.slice(1)) + str[0];**

}

console.log(reverseString("hello"));

// Output: "olleh"
Enter fullscreen mode Exit fullscreen mode

5. Check for Palindrome

Question : Write a function to check if a string is a palindrome.

Code :

function isPalindrome(str) {

** const cleaned = str.toLowerCase().replace(/[^a-z]/g, '');**

** return cleaned === cleaned.split('').reverse().join('');**

}

console.log(isPalindrome("A man, a plan, a canal, Panama"));

// Output: true
Enter fullscreen mode Exit fullscreen mode

Read more... ⇲

JavaScript interview questions & answers with code

JavaScript interview questions & answers with code

favicon atharvgyan.com

Explore more on Atharv Gyan ⇲

React interviews questions and answers with code

React interviews questions and answers with code

favicon atharvgyan.com

Angular interview questions

Angular interview questions

favicon atharvgyan.com

CSS Interview Questions

CSS Interview Questions along with their answers and Code

favicon atharvgyan.com
atharvgyan Article's
30 articles in total
Favicon
Quantum Computing and Software Development
Favicon
Introduction to Quantum Computing
Favicon
Top 5 Emerging Fintech Trends
Favicon
Master React Interviews: Top Questions, Concise Answers, and Code Examples to Ace Your Next Tech Round!
Favicon
Quantum Computing and Cybersecurity
Favicon
JavaScript interview questions & answers with code
Favicon
Binary Search: Binary Search, Efficient Algorithms, Advanced Applications
Favicon
Exploring Python's itertools Module: Unlocking the Power of Iterators
Favicon
Cloud Security Challenges and Encryption, Identity Management, and Compliance
Favicon
Generative AI in Software Development
Favicon
React interviews questions and answers with code
Favicon
Project Based Learning in STEM Bridging Education and Real World Skills
Favicon
Edge Computing Bridging the Gap Between Cloud and IoT
Favicon
Neurotechnology and Brain Computer Interfaces
Favicon
The Future of Augmented Reality
Favicon
JavaScript: Learn Coding Fundamentals
Favicon
A Starter Guide to Data Structures for AI and Machine Learning
Favicon
Exploring Decorators in Python
Favicon
Java Multithreading: Concurrency and Parallelism
Favicon
Angular Interview Essentials Key Concepts and Skills to Ace Your Interview
Favicon
Angular interview questions
Favicon
Advanced State Management Techniques in ReactJS
Favicon
The Future of Neural Technologies
Favicon
The Rise of Low Code Platforms
Favicon
Quantum Computing and its real world applications
Favicon
Cryptocurrency and Tokens in Web3
Favicon
Multimodal Sentiment Analysis: Integrating Text, Images & Audio for Emotion Detection
Favicon
Understanding the Impact of Emotional Intelligence on Student Success
Favicon
A Comprehensive Guide to Cloud Computing
Favicon
Quantum Algorithms

Featured ones: