DEV Community

Cover image for Mastering JavaScript: 10 Tricky Interview Questions on Arrays and Objects
Krishna Pankhania
Krishna Pankhania

Posted on • Updated on

Mastering JavaScript: 10 Tricky Interview Questions on Arrays and Objects

Introduction:

Welcome, fellow developers! In the realm of JavaScript interviews, navigating questions on arrays and objects requires a solid understanding of the language's nuances. In this blog post, we'll delve into 10 tricky interview questions, aiming for clarity and mastery rather than humor. Let's sharpen those coding skills!


1. The Swapping Saga:
Question: How can you swap the values of two variables without using a temporary variable?

Answer: Utilize destructuring assignment for an elegant swap:

let a = 42, b = 24;
[a, b] = [b, a];
Enter fullscreen mode Exit fullscreen mode

The values exchange places seamlessly.


2. Object Overhaul:
Question: Explain the process of cloning an object in JavaScript.

Answer: Employ the spread operator for a simple object duplication:

const originalObj = { one: 1, two: 2 };
const clonedObj = { ...originalObj };
Enter fullscreen mode Exit fullscreen mode

Cloning accomplished without complication.


3. Array Amalgamation:
Question: How do you eliminate duplicates from an array?

Answer: Leverage the Set data structure to effortlessly remove duplicates:

const arrayWithDupes = [1, 2, 3, 1, 2, 4];
const noDupesArray = [...new Set(arrayWithDupes)];
Enter fullscreen mode Exit fullscreen mode

Duplicates vanish, leaving a pristine array.


4. The Palindrome Puzzle:
Question: Write a function to determine if a string is a palindrome.

Answer: Craft a function using array methods:

function isPalindrome(str) {
  const reversed = str.split('').reverse().join('');
  return str === reversed;
}
Enter fullscreen mode Exit fullscreen mode

A reliable palindrome detector.


5. Object Inquiry:
Question: How can you check if an object contains a specific property?

Answer: Employ the "in" operator for a straightforward property check:

const myObject = { name: 'JavaScript', age: 25 };
const hasProperty = 'age' in myObject;
Enter fullscreen mode Exit fullscreen mode

A simple inquiry confirms the property's existence.


6. Array Append:
Question: Add an element to the end of an array.

Answer: Utilize the push() method for seamless array expansion:

const array = ['apple', 'banana', 'cherry'];
array.push('date');
Enter fullscreen mode Exit fullscreen mode

Effortless appending at the array's end.


7. The NaN Dilemma:
Question: How do you determine if a value is NaN?

Answer: Use Number.isNaN() to accurately identify NaN:

const mysteriousValue = NaN;
const isItNaN = Number.isNaN(mysteriousValue);
Enter fullscreen mode Exit fullscreen mode

A reliable check for the elusive NaN.


8. Object Fusion:
Question: Merge two objects into one.

Answer: Utilize the spread operator for a smooth object fusion:

const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };
const fusedObject = { ...obj1, ...obj2 };
Enter fullscreen mode Exit fullscreen mode

Objects combine forces seamlessly.


9. The Filtering Feat:
Question: Remove falsy values from an array.

Answer: Deploy the filter() method to sift out falsy elements:

const questionableArray = [0, false, '', null, undefined, 42];
const truthArray = questionableArray.filter(Boolean);
Enter fullscreen mode Exit fullscreen mode

False values eliminated, leaving truth behind.


10. The Lost Element:
Question: Find the missing number in an array of consecutive integers.

Answer: Utilize the sum formula to deduce the missing number:

const detectiveArray = [1, 2, 3, 5, 6];
const missingNumber = 15 - detectiveArray.reduce((sum, num) => sum + num, 0);
Enter fullscreen mode Exit fullscreen mode

A mathematical approach unveils the missing integer.


Bravo! You've navigated through these JavaScript intricacies. These questions and answers are designed to hone your understanding of arrays and objects, ensuring you're well-prepared for any interview challenge. Happy coding!

Top comments (0)