25

How can I detect if a variable is a string?

Ben
  • 60,438
  • 111
  • 314
  • 488
  • 1
    You might want to use underscore.js, it has methods for this built in http://documentcloud.github.com/underscore/#isString - in case of isString it uses the method mentioned by user113716 – Martin Wawrusch Mar 09 '12 at 15:08
  • 2
    possible duplicate of [Check if a variable is a string](http://stackoverflow.com/questions/4059147/check-if-a-variable-is-a-string) – Philipp Kyeck Feb 23 '14 at 22:06
  • possible duplicate of [Check whether variable is number or string in javascript](http://stackoverflow.com/questions/1303646/check-whether-variable-is-number-or-string-in-javascript) – Jonathan Hall Aug 21 '15 at 20:12

9 Answers9

40

This is the way specified in the ECMAScript spec to determine the internal [[Class]] property.

if( Object.prototype.toString.call(myvar) == '[object String]' ) {
   // a string
}

From 8.6.2 Object Internal Properties and Methods:

The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).


For an example of how this is useful, consider this example:

var str = new String('some string');

alert( typeof str ); // "object"

alert( Object.prototype.toString.call(str) ); // "[object String]"

If you use typeof, you get "object".

But if you use the method above, you get the correct result "[object String]".

user113716
  • 318,772
  • 63
  • 451
  • 440
  • 10
    not sure why this is the accepted answer. objects are not strings. `new String()` results in an object containing a series of indexed properties, each with a value corresponding to the character at that position. Yes, it will get coerced into a string via `Object`'s `toString` method, but the fact remains that `new String('some string')` is not a string, in and of itself. `typeof someString === "string"` is the correct way to determine if a variable is a string. Whether a variable can be *turned into* a string is moot; every object that can trace its prototype chain to `Object` has `toString` – jackwanders Aug 17 '12 at 19:16
23

You can use typeof to do it, but for a lot of things this is bad design.

if (typeof myVar == "string") {
    alert("I'm a string!");
}
Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • Depends on what you're trying to do. Typically you should know what type you're receiving or it shouldn't matter (ie, you'll do the same thing to it regardless of the type). – Daniel DiPaolo Feb 03 '11 at 21:33
10

Use typeof.

if (typeof foo == 'string')
bryc
  • 12,710
  • 6
  • 41
  • 61
simshaun
  • 21,263
  • 1
  • 57
  • 73
5

I don't think we need to treat new String('some string') as a string, because if we try:

new String('abc')==='abc'

It will return false!

But if we try:

String('abc')==='abc'

It will return true.

user1607706
  • 51
  • 1
  • 2
4
var str = new String("some string");
if(str.constructor.name === "String") 
     {
        alert("I'm a string!");
      }

or simply:

if(str.constructor === String) 
     {
        alert("I'm a string!");
      }
The Mask
  • 17,007
  • 37
  • 111
  • 185
0
('str'.constructor === String) && alert('its a string!');
Cody
  • 9,785
  • 4
  • 61
  • 46
0

typeof('yourstring');// returns string

Radek Benkel
  • 8,278
  • 3
  • 32
  • 41
0

Use alert(typeof "hello"); alert(typeof 123);

Ref.: here.

Community
  • 1
  • 1
Adil Mehmood
  • 462
  • 3
  • 13
0

Using underscore:

_.isString(yourVariable)

Underscore is a JS library: https://underscorejs.org/

Diego Cotini
  • 1,039
  • 2
  • 18
  • 25