7

This question is a spin-off of [] is an instance of Array but "" isn't of String

Given that

"" instanceof String; /* false */
String() instanceof String; /* false */
new String() instanceof String; /* true */

and

typeof "" === "string"; /* true */
typeof String() === "string"; /* true */
typeof new String() === "string"; /* false */

Then, if I have a variable abc and I want to know if it's a string, I can do

if(typeof abc === "string" || abc instanceof String){
    // do something
}

Is there a simpler, shorter and native way of doing this, or must I create my own function?

function isStr(s){
    return typeof s === "string" || s instanceof String;
}
if(isStr(abc)){
    // do something
}
9
  • Do you really use new String() in your code or it's just a theoretical question? Commented Sep 3, 2012 at 23:01
  • He might have to deal with other people's code and just wants to be sure he catches all String types without reading (or even having access to) all of it. Commented Sep 3, 2012 at 23:04
  • @zerkms It's a theorical question. But it can be a practical question if two programmers are working together and one uses "" and the other uses new String() Commented Sep 3, 2012 at 23:04
  • 1
    jQuery just uses typeof s === "string" when checking the types of passed in arguments. I suspect this normally works just fine because it's rare for someone to explicitly code a string object from new String(). Commented Sep 3, 2012 at 23:15
  • 1
    possible duplicate of Check if a variable is a string Commented Sep 4, 2012 at 19:20

3 Answers 3

7

I think Object.prototype.toString.call(a) === "[object String]" is the shortest/nativest way of doing this

Sign up to request clarification or add additional context in comments.

1 Comment

Would ( foo.constructor == String ) not be cleaner? Seems to work for me for both cases but I haven't tested in all engines
2

you are correct:

typeof myVar == 'string' || myVar instanceof String;

is one of the best ways to check if a variable is a string.

Comments

1

You may be confused because [] is an array initialiser (often called an array literal) that is defined as creating an Array object, whereas '' is a string literal that is defined as creating a string primitive.

A primitive isn't an instance of any kind of object, though it may be coerced to a related object for convenience.

A more important question is why an isString function should return true for both string primitives and string objects? The use of string objects is (extremely?) rare, I would have thought that their use would infer special treatment and that you would want to differentiate between the two and not treat them the same.

It's far more common to ignore the Type of a variable and, where it's Type might vary, unconditionally convert it to the required Type, e.g. if you want a string primitive:

function foo(s) {
  s = String(s); // s is guaranteed to be a string primitive
  ...
}

The exception is where functions are overloaded and have different behaviour depending on whether a particular argument is a Function, Object or whatever. Such overloading is generally not considered a good idea, but many javascript libraries are dependent on it. In those cases, passing a String object rather than a string primitive may have unexpected consequences.

2 Comments

Precisely this question came to my mind when I was thinking of a function which does different things depending on the type of the argument.
So now you head down the well–trodden path of functions for isFunction, isObject and so on. :-) Javascript's lose typing means such tests can only work in limited cases, so document the limitations and move on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.