JavaScript Array Type [Tech Interview Quiz]
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
|
|
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 thetypeof
operator.There’s no such type as
array
in JS, so it’s impossible to see the messageARRAY!
on the screen.In fact, all JS arrays are objects, so the execution goes into the
else
branch, andSOMETHING WEIRD
is printed on the screen.
Answer
SOMETHING WEIRD
will be logged to the screen as all JS arrays have the typeobject
.