63

In JavaScript, I can declare a string in the following ways;

var a = "Hello World";
var b = new String("Hello World");

but a is not an instance of String...

console.log(a instanceof String); //false;
console.log(b instanceof String); //true;

So how do you find the type or "instanceof" a string literal?

Can JavaScript be forced to create a new String() for every string literal?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • 1
    to answer the actual question, in case you really like using `instanceof` (like me, and hate the `typeof ==` garbage) you can easily coerce things to objects when you check them. Whack this in your code `Object.defineProperty(Object.prototype, '_object', {get:function() { return this; }});` and voila, `'string'._object instanceof String` returns `true`! You can avoid prototype and make functions if you want, but this makes for the most elegant if-statements – Hashbrown Jul 07 '18 at 04:41
  • @Hashbrown This doesn't seem to work on strict mode. Is there any other way to use something like this when using strict mode? – The Real Underscore Aug 08 '21 at 04:34

3 Answers3

133

use typeof "foo" === "string" instead of instanceof.

Artur Udod
  • 4,465
  • 1
  • 29
  • 58
  • 23
    excuse me, could you explain why? – Ninja Coding May 09 '18 at 15:49
  • 2
    @NinjaCoding basically you can check the link provided at the start of the question https://stackoverflow.com/questions/203739/why-does-instanceof-return-false-for-some-literals. You'll find a more detailed explanation. But essentially `String` objects are not primitives, that's the thing. Welcome to js =) – Artur Udod May 18 '18 at 16:04
  • well that's not gonna work if it actually is a `String` (`(function() { return typeof this; }).call('foo')`: `object`), [this'll work for both](https://stackoverflow.com/questions/16792051/how-to-instanceof-a-primitive-string-string-literal-in-javascript#comment89420815_16792051) – Hashbrown Jul 07 '18 at 04:48
  • @NinjaCoding because this answers the first question "So how do you find the type or "instanceof" a string literal?" – user227353 Dec 21 '21 at 02:54
13

Use typeof instead and just compare the resulting string. See docs for details.

isaach1000
  • 1,819
  • 1
  • 13
  • 18
  • 1
    Thanks for the doc link; but [instanceof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof) is just as pertinent: "The instanceof operator tests presence of constructor.prototype in object's prototype chain." – jpaugh Aug 28 '17 at 14:28
5

There is no need to write new String() to create a new string. When we write var x = 'test'; statement, it create the x as a string from a primitive data type. We can't attach the custom properties to this x as we do with object literal. ie. x.custom = 'abc'; x.custom will give undefined value. Thus as per our need we need to create the object. new String() will create an object with typeof() Object and not string. We can add custom properties to this object.

Michael W
  • 690
  • 1
  • 9
  • 22