Coderslang

Search IconIcon to open search

JavaScript Array Type [Tech Interview Quiz]

Last updated Jan 7, 2023

Arrays are a fundamental data type in JavaScript, and they are essential for storing and manipulating collections of data. Whether you’re working with a list of numbers, strings, or objects, arrays offer a powerful and versatile way to represent and manage data.

In the following code snippet an interviewer would test your knowledge about the array type in JavaScript.

Code snippet as a picture

1
2
3
4
5
6
const array = [ 'this', 'is', 'an', 'array' ];
if (typeof array === 'array') {
  console.log('ARRAY!')
} else {
  console.log('SOMETHING WEIRD...')
}

What’s going to be the output? Is array an array? (think well, then click for the answer and explanation)

Explanation

In line one we create an array and bind it with the array constant. Then the type of the value of this constant is evaluated by the typeof operator.

There’s no such type as array in JS, so it’s impossible to see the message ARRAY! on the screen.

In fact, all JS arrays are objects, so the execution goes into the else branch, and SOMETHING WEIRD is printed on the screen.

Answer

SOMETHING WEIRD will be logged to the screen as all JS arrays have the type object.

🔥 START LEARNING 🔥