DEV Community

Cover image for JavaScript interview questions & answers with code
Atharv Gyan
Atharv Gyan

Posted on

1 1 1 1 1

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

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay