Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why is "a string" instanceof String false?

Do you know what will be the output of this code?

JavaScript
console.log("This is a string." instanceof String);
Show answer and explanation

This actually returns false.

The reason for that circumstance is that JavaScript distinguishes between primitives and objects.

And “This is a string.” is actually a primitive string and not an instance of the object String.

If the code was like this:

new String("This is a string.") instanceof String

you’d actually get the result you would have expected at the beginning.

What instanceof actually does is checking if the String constructor is nested within the prototype chain of the value provided.

In this case, it isn’t.

Take the full quiz Next question →

More Strings questions

  1. Why does ('b' + 'a' + + 'a' + 'a') return "banana"?
  2. Why does 1 + "2" + "2" return "122"?
  3. What does String.raw do in JavaScript?
  4. Why is "10" < "9" true in JavaScript?
  5. Can you change a character in a JavaScript string?

Related topics