Search⌘ K

Solution Review: "instanceof" Operator

Explore how the instanceof operator works in JavaScript to check object inheritance and prototype chains. Understand its application with arrays and string objects to prepare for related interview questions in frontend development.

Question 1: Solution review #

Explanation #

The instanceof operator checks if an operand is an instance of the object passed on the right or of any of its ancestors in its prototype chain.

Run the code below:

Javascript (babel-node)
var names = ["Tom","Anna",2,true]
console.log(names instanceof String)
console.log(names instanceof Number)
console.log(names instanceof Object)
console.log(names instanceof Array)

From the output above, you can see that Option D is correct because in the code we have an array, names ...