最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Easy way to check if a variable is a string? - Stack Overflow

programmeradmin1浏览0评论

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
}

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
}
Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked Sep 3, 2012 at 23:00 OriolOriol 288k70 gold badges456 silver badges530 bronze badges 9
  • Do you really use new String() in your code or it's just a theoretical question? – zerkms 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. – Vala 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() – Oriol 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(). – jfriend00 Commented Sep 3, 2012 at 23:15
  • 1 possible duplicate of Check if a variable is a string – tripleee Commented Sep 4, 2012 at 19:20
 |  Show 4 more ments

3 Answers 3

Reset to default 7

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

you are correct:

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

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

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 mon 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.

发布评论

评论列表(0)

  1. 暂无评论