JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – can really instanceof operator work on string literal

JavaScript Quiz – can really instanceof operator work on string literal

instanceof operator –

The instanceof operator is used to checking the type of an object at runtime. The instanceof operator returns a boolean value which indicates if an object is an instance of a particular class.

It’s syntax looks like –

object instanceof constructor

instanceof operator is also looking for prototype property of constructor appear anywhere in a prototype chain of the object.

To know more about JavaScript instanceof click here.

JavaScript Question

 In this javascript quiz code, you need to find out the value return from instanceof operator when checking against array and string.

First, we create a variable array using let keyword and assign an array to it. Which contain 2 elements.

let array = [“JS”, “Startup”];

Now, in the next step create another variable website and store a string in it.

let website = “jsstartup.com”;

Now, we check the instanceof array variable against Array and it give us a true.

console.log(array instanceof Array); // true

Finally, the question arises that what should be the output of the other two console log below.

console.log(website instanceof String); // ?

console.log(array instanceof Object); // ?

Answer – false, true

Click To Run Code

Explanation

Now, you the answer is false, true but why this is answer. Let me explain

So First, we check the instanceof website variable which is a string but still gives a false. Because we assign String literal to website variable. As string literal is not an object.

Now, why array variable give true against object also because it’s an array and instanceof Array is also true.

Note: As you know typeof array is also an object.

So, you are checking array constructor and it is a function to create arrays. And Array is also an instanceof function.

Array instanceof Function

And function is also an objects. That’s why it gives us true

You can check this stackoverflow answer – click here.

Finaaly, That’s why we get an answer “false, true“.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – Did really string substring method can do this
  2. JS Quiz – Did really the reference pass using Array.from() method
  3. JS Quiz – Is the String() and toString() are really same
  4. JavaScript Quiz – can in operator check value or index in an array

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link