JavaScript Array Interview Questions

Umar Farooque Khan
3 min readJul 12, 2023

--

JavaScript array interview questions often test your understanding of array manipulation, iteration, and various array methods. Here’s a brief overview of some common JavaScript array interview questions:

Question -1

How can you double elements of an array? Do not use extra variable?

Solution:

arr.reduce((accumulator, currentValue, index, array)=> array[index -1] =array[index -1] *2  )

Question -2

How to check if given input is string ?

Solution:

Object.prototype.toString.call(new String()) ==='[object String]'

Question -3

How to check if given input is array ?

Solution:

Object.prototype.toString.call(new Array()) ==='[object Array]'

Question -4

What will be the result when executing the given code?

const arr = ['A', 'N', 'U'];
arr[10] = 10;
console.log(arr.length);

Output:

11

Explanation:

The array arr is initialized with elements and an assignment at index 10. Despite gaps in indices, arr.length reflects the highest index plus one. Thus, the output is '11'.

Question -5

What is the output of the following code?

let array = [1, 2, 3, 4, 5];
array.length = 0;
console.log(array)

Output:

[]

Explanation:

The code sets the length of the array to 0 using array.length = 0, effectively clearing all elements from the array. As a result, when you log the array to the console (console.log(array)), you'll get an empty array ([]).

Question -6

How can you extract and print the unique values from an array?

Solution:

const getUniqueValues = (arr) => [...new Set(arr)];

Explanation:

The getUniqueValuesutilizes the Set data structure to automatically remove duplicate values. The spread operator (...) is then used to convert the unique values back into an array.

Question -7

How can you determine if a number is an integer in JavaScript?

Solution:

const isInt = (num) => num % 1 === 0;

Explanation:

The function utilizes the modulo operator (%) to check if the remainder when dividing by 1 is zero, indicating an integer.

Question 8:

What is the output of the following code?

let arr1 = [10, 12, 3.1];
let arr2 = [10, 12, 3.1];
console.log(arr1 == arr2);

Output:

false

Explanation:

Arrays are reference types in JavaScript, so arr1 and arr2 point to different references, even though their contents are the same. Therefore, arr1 == arr2 will be false.

Question 9:

What is the output of the following code?

let originalArray = [1, 2, 3];
let copiedArray = originalArray.slice();
copiedArray.push(4);
console.log(originalArray);
console.log(copiedArray);

Output:

[1, 2, 3]
[1, 2, 3, 4]

Explanation:

In the provided JavaScript code, originalArray and copiedArray initially reference different array objects due to the use of the slice() method, which creates a shallow copy. Therefore, modifying copiedArray by pushing the value 4 does not affect the underlying array that originalArray references. As a result, when you log originalArray to the console, it remains unchanged, and you will see the output [1, 2, 3]. Conversely, logging copiedArray reflects the addition of the value 4, resulting in the output [1, 2, 3, 4].

Question 10:

What is the output of the following code?

Output:

true
false

Explanation:

JSON.stringify plays a key role by transforming arrays into a consistent string format, aiding in effortless comparison. This approach simplifies the equality check process and enhances code readability, making it a convenient choice for array comparison in JavaScript.

Conclusion:

In conclusion, JavaScript array interview questions often cover a range of topics related to array manipulation, iteration, and understanding various array methods. It is important to be familiar with common array operations such as adding and removing elements, iterating over arrays, checking for the existence of elements, and finding the length of an array. Additionally, knowledge of concepts like unique values and emptying arrays can also be beneficial. By having a good understanding of these array-related concepts and practicing with coding examples, you can effectively tackle JavaScript array interview questions.

Here are some additional high-quality tutorials for you to explore:

  1. JavaScript interview Question and Answer
  2. Node Js Interview Question and Answer
  3. JavaScript Tricky Question

--

--

Umar Farooque Khan
Umar Farooque Khan

Written by Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.

Responses (9)